[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Why this C++ polymorphism does not work?

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

649 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