Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

Why this C++ polymorphism does not work?

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.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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()
{
...
}
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
Avatar of prain

ASKER

Thanks. It worked.