3
//Parent.h
class Parent
{
public:
    virtual void foo() = 0;
};

//Child.h
class Child : public Parent
{
public:
    virtual void foo(){cout << "inside function foo()" << endl;}
    virtual void bar(){cout << "inside function bar()" << endl;};    
};

int main( int argc, char** argv ){
    Parent* pa = new Child;
    pa->foo();
    pa->bar(); //Error, class Parent have no member bar
    return 0;
}

How do i avoid this error other than add those function to the Parent class

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
slier
  • 6,511
  • 6
  • 36
  • 55

4 Answers4

3

You can declare your pointer as Child type. You will still be able to pass it to function expecting a Parent pointer.

Other solution involves using dynamic_cast<Child>(pa) although this is often seen as bad design to need this because it defeats what polymorphism is supposed to accomplish.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
1

You can use static or dynamic_cast. Example with dynamic_cast:

if (Child* child = dynamic_cast<Child*>(pa))
    child->bar();
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
1

You avoid this error by being more careful in your class design in the first place.

Yes, you can use dynamic_cast or static_cast to cast a Base pointer to a Derived pointer if the types are related, as they are here. But let's think about this.

Q: Why do you use polymorphism? A: To provide different behaviour for similar operations, where the behaviour is selected at run-time.

Polymorphism is used to abstract away the implementation of an operation from the provision of that operation. All Shapes for example have a number of sides. How many sides depends on the actual shape, but we shouldn't have to know what kind of Shape something is in order to ask it how many sides it has. We should be able to just ask it.

Q: Why do you typically need to use dynamic_cast?
A: Because the base pointer doesn't provide the facilities you need.

If we shouldn't have to care what kind of Shape some object is in order to carry out the operations we need on it, then why would the Shape interface ever not provide whatever facilities we need?

This happens when we've made a mistake in designing something. Either Shape didn't have enough or the right kinds of facilities in it, or some particular Shape subclass is trying to do more than it should.

That's what you've done here. If it doesn't make sense for Parent to have a (public) bar() method on it, then it shouldn't make sense for Child to have it either. If it does make sense for Child to have a bar method on it, then Child isn't really a Parent, is it?

Maybe bar should be a method in some other class.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

First we need to know why compiler is complaining !! You will never come to know the reason if anybody tell you the fix. First understand the reason.

You are getting the error during compilation. vTable and vPtr does not come into picture during early binding. It comes into picture in late binding. Base class does not have bar method, so compiler is complaining you that the method called by base class in unknown. So you need to typecast it to derived class pointer so that early binding gets passed. I hope my understand about the problem is correct. Correct me if I am wrong.

niloydebnath
  • 111
  • 1
  • 6