Solved

Why this C++ polymorphism does not work?

Posted on 2012-04-04
4
300 Views
Last Modified: 2012-04-05
I am creating a C++ Abstract wrapper where internally I call boost::thread class
functions. My intention is to call the run() of each of derived classes of this (java style)
So I define the run() as pure virtual. So here is the base Thread class.

//Thread.h
#include <boost/thread.hpp>
#include <boost/lambda/bind.hpp>
class Thread
{
    public:          
        Thread();
        virtual ~Thread();                
        void spawn();

        protected :
        virtual void run() = 0;
        boost::thread* mThread;    
};


//Thread.cpp
#include <boost/bind.hpp>
#include "Thread.hh"

 Thread::Thread() : mThread()
    {
      std::cout << "Passed Constructor "<< std::endl;
    }

  Thread::~Thread()
    {   }


  void Thread::spawn()
    {
        if (mThread == NULL)
        {
             mThread = new boost::thread(
                    boost::lambda::bind(&Thread::run, this));
        }
        else
        {
            std::cout <<"Error while creating Threa" << std::endl;
        }
    }
=========================================================================
Then now I create a derived of the above.

#include "Thread.hh"
class DerivedThread : public Thread
{
   public:  
   DerivedThread ();
   
    virtual ~DerivedThread();
    virtual void run();
}


//"DerivedThread.cpp

#include "DerivedThread.hh"

  DerivedThread::DerivedThread  (){}
  DerivedThread ::~DerivedThread();

  void DerivedThread ::run() {
        std::cout << "in run() "<< std::endl;

        for (int i = 0; i < 10; i++){
          std::cout << "Running...." << i << std::endl;
        }
    }

================================================================================

//TestDriver.cpp
#include "Thread.hh"
#include "DerivedThread.hh"

int main()
{
  Thread* aThread1 = new DerivedThread();
  aThread1->join();

  aThread1->spawn();    
}

//I expected here to print the lines in run() in DerivedThread. It did not happen
//Will the run() eleborated in DerivedThread be binded to the thread in spawn().


BTW: If I move the eleborated run() to the Thread class and remove virtuality from
the run() in Thread class, I see the printed lines.

Can someone please help me here.
0
Comment
Question by:prain
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 37808513
The 'run()' in your base class is preotected, the one in the derived class isn't - move them to the same protection scope and it should work.
0
 
LVL 22

Expert Comment

by:ambience
ID: 37811029
My guess is that boost bind is not polymorphic,

boost::lambda::bind(
&Thread::run

Open in new window

, this)

You can try do it this way

class Thread
{
private:
       void run() {
             runInternal();
       }
protected:
       void runInternal() = 0;
}

DerivedThread::runInternal()
{
...
}
0
 
LVL 32

Expert Comment

by:sarabande
ID: 37811680
ambience is right. boost::bind takes function pointer and it is not possible to make a polymorphic call via function pointer.

note, the function pointer passed to boost::bind already allows you to pass run function of the derived class. you could try to move the bind call into the derived classes by virtual call in Thread::spawn().

Sara
0
 

Author Closing Comment

by:prain
ID: 37812119
Thanks. It worked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now