Virtual Member Function

  • Thread starter jean.daniel.michaud
  • Start date
J

jean.daniel.michaud

Hi,

I am using a class with operator() as threadfunc for Boost.Thread. I
have inheritance used with those class but it does not work, see the
example:

// Snippet on
#include <iostream>
#include <vector>
#include <boost/thread.hpp>

class Bird
{
public:
virtual void operator()() {std::cout << "a bird" << std::endl;}
virtual ~Bird() {}
};

class Swan : public Bird
{
public:
virtual void operator()() {std::cout << "a swan" << std::endl;}
};

int main()
{
Swan mySwan;
Bird* myBird = &mySwan;

Bird *b = &mySwan;
boost::thread thrd(*b);
thrd.join();

return 0;
}
// Snippet off

I would like to see "a swan" displayed, but I get "a bird"
instead... and for some reason I don't want to do that:
boost::thread thrd(mySwan);

As the member function is virtual, on call of operator () on the
object b (which is a swan) I should see Swan::eek:perator() called, no?
What it it I don't understand here?

Thanks and regards,

JD
 
D

dasjotre

Hi,

I am using a class with operator() as threadfunc for Boost.Thread. I
have inheritance used with those class but it does not work, see the
example:

// Snippet on
#include <iostream>
#include <vector>
#include <boost/thread.hpp>

class Bird
{
public:
virtual void operator()() {std::cout << "a bird" << std::endl;}
virtual ~Bird() {}

};

class Swan : public Bird
{
public:
virtual void operator()() {std::cout << "a swan" << std::endl;}

};

int main()
{
Swan mySwan;
Bird* myBird = &mySwan;

Bird *b = &mySwan;
boost::thread thrd(*b);

when you dereference b, what you get is the Bird part of mySwan
object.

try instead
boost::thread thrd(boost::bind(&Bird::eek:perator(), b));

or if you have TR1
::boost::thread thrd(std::tr1::bind(&Bird::eek:perator(), b));
 
M

Markus Moll

Hi

(e-mail address removed) schreef:
I am using a class with operator() as threadfunc for Boost.Thread. I
have inheritance used with those class but it does not work, see the
example:
[includes, class Bird, class Swan snipped]
int main()
{
Swan mySwan;
Bird* myBird = &mySwan;

Bird *b = &mySwan;
boost::thread thrd(*b);

From the boost documentation:

| class thread : private boost::noncopyable // Exposition only
| {
| public:
| // construct/copy/destruct
| thread();
| explicit thread(const boost::function0<void>&);
| ~thread();

and

| template<typename F> functionN(F f);
| Requires: F is a function object Callable from this.
| Postconditions: *this targets a copy of f if f is nonempty, or
| this->empty() if f is empty.

So effectively you create a boost::function0<Bird> object that _copies_
*b (slicing!). You then create a boost::thread object from that.

Avoid the copy by using an additional indirection, e.g. consider
boost::ref :

boost::thread thrd( boost::ref(*b) );

Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top