Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding function in C++ doesn't work

#include <cstdio>
using namespace std;

class A {
public:
    virtual void func() { printf("A::func()"); }
};

class B : public A {
public:
    virtual void func() { printf("B::func()"); }
};

int main() {
  A a = *(A *)new B();
  a.func();
}

The question is simple: why a->func() calls function in class A even though a contains object of class B?

like image 934
mnn Avatar asked Jan 01 '11 14:01

mnn


People also ask

Is function overriding possible in C?

If derived class defines same function as defined in its base class, it is known as function overriding in C++. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of the function which is already provided by its base class.

How is function overriding implemented in C?

How to access Overridden Functions in C++ 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.

What is the problem associated with function overriding?

Depending upon the caller object, proper function is invoked. - As seen above, the derived class functions override base class functions. The problem with this is, the derived class objects can not access base class member functions which are overridden in derived class.

Does C++ support overriding?

Function Overriding (achieved at run time) It is the redefinition of base class function in its derived class with same signature i.e return type and parameters. It can only be done in derived class.


1 Answers

A a = *(A *)new B();
a.func();

Here's what happens in this code, step by step:

  • new B(): a new object of type B is allocated on the free store, resulting in its address
  • (A*): the address of the object is cast to A*, so we have a pointer of type A* actually pointing to an object of type B, which is valid. All OK.
  • A a: here the problems start. A new local object of type A is created on the stack and constructed using the copy constructor A::A(const A&), with the first paremeter being the object created before.
  • The pointer to the original object of type B is lost after this statement, resulting in a memory leak, since it was allocated on the free store with new.
  • a.func() - the method is called on the (local) object of class A.

If you change the code to:

A& a = *( A*) new B();
a.func();

then only one object will be constructed, its pointer will be converted to pointer of type A*, then dereferenced and a new reference will be initialized with this address. The call of the virtual function will then be dynamically resolved to B::func().


But remember, that you'd still need to free the object since it was allocated with new:

delete &a;

Which, by the way, will only be correct if A has a virtual destructor, which is required that B::~B() (which luckily is empty here, but it doesn't need to in the general case) will also be called. If A doesn't have a virtual destructor, then you'd need to free it by:

delete (B*)&a;

If you would want to use a pointer, then that's the same as with the reference. Code:

A* a = new B(); // actually you don't need an explicit cast here.
a->func();
delete (B*)a; // or just delete a; if A has a virtual destructor.
like image 71
Kos Avatar answered Sep 30 '22 16:09

Kos