473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Virtual Member Function

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::operator() called, no?
What it it I don't understand here?

Thanks and regards,

JD

May 25 '07 #1
3 1380
On 25 May, 11:02, jean.daniel.mich...@gmail.com wrote:
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::operator(), b));

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


May 25 '07 #2
Hi

je*****************@gmail.com 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 FfunctionN(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<Birdobject 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
May 25 '07 #3
Markus, dasjotre,

Both you solutions work. Thanks a lot!

JD

May 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.