使用带有boost :: thread数据成员的类的子类时出错

时间:2014-12-04 01:36:39

标签: c++ subclass boost-thread data-members

我创建了以下类

#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>

class Messaging
{
public:
    Messaging(const std::string& newMessage, unsigned long long maxTimeToDeliver = 12): m_theMessage(newMessage), m_maxTimeToDeliver(maxTimeToDeliver), athread(){};
    virtual ~Messaging(void);

protected:
    std::string m_theMessage;
    unsigned long long m_maxTimeToDeliver;
    boost::thread athread;
};

和子类

#include "messaging.h"
#include <iostream>

class SportMessaging :public Messaging
{
public:
    SportMessaging(const std::string newMessage, unsigned long long maxTimeToDeliver = 1): Messaging(newMessage, maxTimeToDeliver) {};
    virtual ~SportMessaging(void);
};

在主要内容中我尝试创建每个

的对象
#include "SportMessaging.h"
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
int main()
{
    SportMessaging anotherSportMessagingObject = SportMessaging("another sports message",4);  //gives error C2248: 'boost::thread::thread' : cannot access private member declared in class 'boost::thread'

    Messaging aMessObj = Messaging("sports message 3");  //works

    return 0;
}

为什么我可以创建Messaging对象,但SportMessaging对象失败了?

我环顾四周并怀疑它可能与boost :: thread有一个类似于boost :: thread_group(stackoverflow post on using boost::thread_group in a class)的私有拷贝构造函数。

但是,似乎Messaging("sports message 3")SportMessaging("another sports message",4)都会调用它们的复制构造函数,然后调用每个非静态成员的复制构造函数(即包括尝试调用boost :: thread的复制构造函数) )。最后一句中的想法与'SportMessaging anotherSportMessagingObject = SportMessaging(“另一项体育讯息”,4)的事实相冲突;不起作用和'Messaging aMessObj = Messaging(“sports message 3”);`有效。

0 个答案:

没有答案