Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to override a function in C++ child class without using virtual keyword to the function in parent class which is abstract?

class Parent {
public:
    void func1(); // Complete meaningful definition in parent given.

    virtual HRESULT func2()=0;  // Bcoz of this function Parent class is abstract. 
};


class Child: public Parent {
public:
    void func1(); // Different definition in child.
};

Is this possible in C++ ? I am overriding func1() which is NOT virtual and it already has a definition in parent abstract class.

like image 452
codeLover Avatar asked Mar 08 '12 15:03

codeLover


2 Answers

[assuming here Child extends Parent, unlike what the code snap shows]

Yes it is possible [it is called hiding] - but you will not get a dynamic dispatch behavior.

The static type will define which method will be invoked, and not the dynamic type.

For example:

Parent* p = new Child;
p->func1();

Will invoke Parent::func1()
while:

Child* c = new Child;
c->func1();

Will invoke Child::func1()

like image 66
amit Avatar answered Oct 09 '22 18:10

amit


No, it's not possible to actually override the definition in the parent (at least when talking about C++, "override" is normally reserved specifically to referring to virtual functions). Instead, defining a function with the same name in the child class simply hides the function in the parent that has the same name (i.e., in the context of a child object, looking for that name will only find the function in the child, not the one in the parent).

If you want to (and the functions have different signatures) you can also get the functions in both the parent and the child treated as overloaded, so a call will try to call whichever matches better:

struct parent {
    void func1(char) {}
};

struct child : public parent { 
    void func1(long) { }

    using parent::func1;
};

Now, you get:

child c;
c.func1('a'); // calls parent::func1
c.func1(123L); // calls child::func1

This is yet a third type of behavior though, different from having a virtual function or having a function in the child that hides the one in the parent.

With a virtual function, the selection of which function is called is based on the dynamic type, so if you have a pointer/reference to the base class, the function called depends on whether that refers to an object of the base or derived class.

When you hide the function, the function that's called is based on the static type, so if you call it via a pointer/reference to the base, it calls the base function, even if that actually refers to an object of the derived class. If, however, you use a pointer or reference to (or directly use an instance of) the derived class, it'll invoke the function in the derived class.

With the using statement, you get function overloading, so when you call the function (in the context of the derived class) the function that's called is based on which function's signature is the best match for the parameter(s) you pass.

like image 37
Jerry Coffin Avatar answered Oct 09 '22 16:10

Jerry Coffin