Solved

Why this C++ polymorphism does not work?

Posted on 2012-04-04
4
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

756 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