?
Solved

Why this C++ polymorphism does not work?

Posted on 2012-04-04
4
Medium Priority
?
315 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 600 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 35

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

741 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