可变参数表 C语言 多态,具有可变函数参数的C多态性

我正在与您分享我使用可变参数函数参数的类所遇到的问题 . 它是以下代码中显示的类Thread . 它是std :: thread的包装器,以便使用函数模式 .

我想在这个函数中使用多态来将类Thread继承到一个新类Functor中,但是gcc会返回以下错误:

#include

#include

using namespace std;

template

class Thread

{

public:

virtual void operator()(Args...) = 0;

void run(Args... args)

{

std::thread t(std::forward< Thread >(*this), std::forward(args)...);

t.join();

}

};

template

class Functor : public Thread

{

public:

// generates the errors bellow

virtual void operator()(Args... /*args*/)

{

}

// doesnot work since the pure virtual function wants another prototype of function.

// void operator()(int)

// {

// }

};

int main()

{

int a = 12;

Functor f;

f.run(ref(a));

return 0;

}

from t-Thread-args2.cpp:1:

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple: In instantiation of ‘struct std::_Head_base, false>’:

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple:215:12: required from ‘struct std::_Tuple_impl, int>’

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple:507:11: required from ‘class std::tuple, int>’

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/functional:1601:39: required from ‘struct std::_Bind_simple(int)>’

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/thread:133:9: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = Thread; _Args = {int}]’

t-Thread-args2.cpp:14:83: required from ‘void Thread::run(Args ...) [with Args = {int}]’

t-Thread-args2.cpp:42:17: required from here

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple:166:13: error: cannot declare field ‘std::_Head_base, false>::_M_head_impl’ to be of abstract type ‘Thread’

t-Thread-args2.cpp:7:7: note: because the following virtual functions are pure within ‘Thread’:

t-Thread-args2.cpp:10:18: note: void Thread::operator()(Args ...) [with Args = {int}]

我真的不理解错误,因为纯虚函数在派生类中得到了很好的定义 . 但是,在将函数run()移动到派生类(Functor)中时,它可以工作 .

提前谢谢,卡纳

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值