Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling derived class function from base class

Tags:

class base {   public:   virtual void start();   virtual void stop();    void doSomething() { start(); .... stop(); } }  class derived : public base {   public:    void start();    void stop(); } 

But when I call doSomething() in the derived class it is using it's own definition of Start() and Stop() - not the derived ones.

I don't want to rewrite doSomething() in the derived class because it would be identical to the base one. What am I doing wrong?

Sorry if that wasn't clear.
The behaviour of Start() and Stop() in the derived class is different (it's a different machine) - but I want to use the original base class doSomething() because that hasn't changed. It just has to start() and stop() using the new derived class code.

like image 477
cpp help Avatar asked Feb 01 '11 23:02

cpp help


People also ask

Can we call derived class function from base class?

Even though the derived class can't call it in the base class, the base class can call it which effectively calls down to the (appropriate) derived class. And that's what the Template Method pattern is all about.

How do you call an inherited class function?

A derived class is created, which is inheriting parent class p1 and overloading the parent class function first(). class d1 : public p1 { public: void first() { cout << "The derived class d1 function is called."; p1::first(); } }; The function of d1 class is calling the function of p1 class.

How do you derive from base class?

The Base class members and member functions are inherited to Object of the derived class. A base class is also called parent class or superclass. Derived Class: A class that is created from an existing class. The derived class inherits all members and member functions of a base class.

How do you invoke a base member function from a derived class in which you have overridden that function?

You must use the scope resolution operator, “::” to access the overridden function. Another way to access the overridden function is by using the pointer of the base class to point to an object of the derived class and calling the function through the pointer.


1 Answers

The code you've posted should work the way you want. Calling doSomething on an instance of derived will call the overridden start and stop functions defined in derived.

There's an exception to that, though. If you call doSomething in the constructor or destructor of base (whether directly or indirectly), then the versions of start and stop that get called will be the ones defined in base. That's because in those circumstances, you don't actually have a valid derived instance yet. It's either not fully constructed or partially destructed, so the language prevents you from calling methods that would use the partial object.

If you're not calling it from a base constructor or destructor, then there is more to the problem than what's shown here.

like image 137
Rob Kennedy Avatar answered Oct 03 '22 13:10

Rob Kennedy