0

I have created the following class

#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;
};

and the subclass

#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);
};

and in main I try to create an object of each

#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;
}

Why can I create the Messaging object, but the SportMessaging object fails?

I've looked around and suspect it might be related to boost::thread having a private copy-constructor similar boost::thread_group (stackoverflow post on using boost::thread_group in a class).

However, it seems like both Messaging("sports message 3") and SportMessaging("another sports message",4) would call their copy-constructor and then each of the nonstatic members' copy-constructor (i.e. including trying to call boost::thread's copy-constructor). The idea in the last sentence conflicts with the fact that 'SportMessaging anotherSportMessagingObject = SportMessaging("another sports message",4);' doesn't work and 'Messaging aMessObj = Messaging("sports message 3");` works.

Community
  • 1
  • 1
user3731622
  • 4,844
  • 8
  • 45
  • 84
  • 1
    I can't tell what's happening in VS (which version are you using). In G++ 4.9.1 the copy constructor for both Messaging and SportMessaging are deleted. Both lines in `main` give the same result, which is "error: no matching function for call to ‘Messaging::Messaging(Messaging)’", or when compiled with --std=c++11 a much more detailed error message explaining that the copy constructor has been implicitly deleted because of the deleted copy constructor in boost::thread. – Jay Miller Dec 04 '14 at 03:00
  • Also not an answer, but if you have a non-copyable member you actually don't want to copy the message objects. Just declare variables like `SportsMessaging object("another sports message", 4);`. – Jay Miller Dec 04 '14 at 03:06
  • @JayMiller I'm using VS2012, but I'm not using c++11. Sounds like c++11's ability to define functions as deleted helps provide better error messages like the one you mentioned when you compiled with --std=c++11. – user3731622 Dec 04 '14 at 20:40
  • @JayMiller Regarding your second comment, I was trying to avoid creating a bunch of SportMessaging objects. I wanted to be able to create one sport messaging object (e.g. `SportsMessaging object("another sports message", 4);` ) and then use copy-assignment to change the object (e.g. `object = SportsMessaging("different sportmessage", 4);`. I now know this might not be a good ideas if the class has a non-copyable member. – user3731622 Dec 04 '14 at 20:47

0 Answers0