首页 / C++ / c++纯虚函数在父类中调用的规避
c++纯虚函数在父类中调用的规避
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c++纯虚函数在父类中调用的规避,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2067字,纯文字阅读大概需要3分钟。
内容图文
构造和析构函数不允许调用纯虚函数,可以先调用虚函数,里面再调用纯虚函数实现。
class
Base{
public
:
virtual
void
foo()=
0
;
Base() { call_foo();}
void call_foo() { foo(); }
};
class
Derived: Base{
void
foo() { }
};
int
main() {
Derived d;
}
在父类中定义纯虚函数,实现工厂生产。子类再实现。可以用虚函数里面调用纯虚函数实现。
父类实现了线程,子类实现方法即可示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
//============================================================================
// Name : mytestcpp.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using
namespace
std;
#include <string>
#include <iostream>
#include "pthread.h"
using
namespace
std;
class
ITestComponent {
public
:
virtual
void
* thread_run(
void
* para)=0;
virtual
void
* thread_run_tmp(
void
* para) {
thread_run(para);
}
;
virtual
void
start() {
typedef
void
* (*FUNC)(
void
*);
//定义FUNC类型是一个指向函数的指针,该函数参数为void*,返回值为void*
FUNC callback = (FUNC) &ITestComponent::thread_run_tmp;
//强制转换func()的类型
int
rc;
pthread_t thread_instance;
pthread_attr_t attr;
// Thread attribute
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&thread_instance, &attr, callback,
this
);
if
(rc) {
cout <<
"thread_instance thread ERROR; return code is "
<< rc << endl;
}
pthread_attr_destroy(&attr);
}
protected
:
};
class
HardwareMeter:
public
ITestComponent {
public
:
void
* thread_run(
void
*para) {
while
(1) {
cout <<
"HardwareMeter "
<<
" start."
<< endl;
}
}
};
int
main() {
cout <<
"!!!Hello World!!!"
<< endl;
// prints !!!Hello World!!!
HardwareMeter hm;
hm.start();
cin >> i;
return
0;
}
|
原文:http://www.cnblogs.com/bigben0123/p/3605263.html
内容总结
以上是互联网集市为您收集整理的c++纯虚函数在父类中调用的规避全部内容,希望文章能够帮你解决c++纯虚函数在父类中调用的规避所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。