dynamic_cast fails when cast a base class to derived class - c++

I have two classes, base class and a derived class.
The base class has a virtual method.
Here is my test example:
class Base
{
public:
virtual void Hello() { cout << "-> Hello Base" << endl; }
};
class Derived: public Base
{
public:
void Hello() { cout << "-> Hello Derived" << endl; }
};
int main()
{
Base *mBase = new Base;
// something to do
....
Derived *mDerived = dynamic_cast<Derived*>(mBase);
mDerived->Hello();
return 0;
}
I'm looking to use the Hello() method of the class derived after the cast of mBase to mDerived.
But the problem is that when I try to use dynamic_cast it will crash the application, if not if I use reinterpret_cast the Hello() method of the Base class will be called.
Result in the case dynamic_cast:
Segmentation fault (core dumped)
Result in the case dynamic_cast:
-> Hello Base

dynamic_cast fails when cast a base class to derived class
This is what is supposed to happen. When you dynamic cast a pointer to an object whose dynamic type is not the casted type, then you get a null pointer as the result.
In your example, you indirect through the null pointer and attempt to call a member function which results in undefined behaviour.
When using dynamic cast, you must always check whether you got null or not.
if I use reinterpret_cast...
Then the behaviour will still be undefined because you'll be indirecting through a pointer to an object that does not exist. Unless you create an instance of the derived class, you cannot call its non static member function.
You can convert a base instance into a derived one like this for example:
Base b;
Derived d = b;
What happens is that the base sub object of the derived instance is copy initialised from b.

Two problems in your code: Base should have a virtual destructor so Dervied isntances can be properly destructed via a pointer to Base. Then, you are not constructing an object of type Derived, so the cast cannot succeed. dynamic_cast can fail and you should check its result for not being a nullptr. Also you forgot to delete the created object.
If you do create an instance of type Derived your code works:
#include <iostream>
using std::cout;
using std::endl;
class Base
{
public:
virtual void Hello() { cout << "-> Hello Base" << endl; }
virtual ~Base(){}
};
class Derived: public Base
{
public:
void Hello() { cout << "-> Hello Derived" << endl; }
};
int main()
{
Base* mBase = new Derived;
Derived *mDerived = dynamic_cast<Derived*>(mBase);
if (mDerived) mDerived->Hello();
delete mBase;
}

Related

The constructor of base class is not called by derived class when base class pointer is used

Why the base class constructor is not called twice in the following code?
#include<iostream>
using namespace std;
class base{
public:
base(){cout << "In the Base Constructor\n"; }
~base(){
cout << "In Base Destructor\n";
}
};
class derived: public base{
public:
derived(){cout << "In Derived Constructor\n";}
~derived(){
cout << "In Derived Destructor\n";
}
};
int main(){
base *bptr;
derived d;
}
output:
In the Base Constructor
In Derived Constructor
In Derived Destructor
In Base Destructor
It must be because of the pointer. But I am not clear why?
This is a pointer to something.
base *bptr;
It is not initialised.
It does not point to anything clean.
It especially does not point to any already created object.
Even if it did, that would not cause any constructor to be executed.
If there would be any object the pointer points to, then the creation of that object would have happened elsewhere, earlier.
This defines an object:
derived d;
This is what causes the observed execution of constructors.

I design a class and base pointer is created but it give error when I try to access a derived function through base pointer

I create a base pointer and pass the address of the derived object to it. But an error displayed when I tried to access a function named fun() through that pointer. Why does this code give errors?
#include<iostream>
using namespace std;
class Base
{
public:
virtual void fun ()
{
cout << "Base::fun()"<< endl;
}
};
class Derived : public Base
{
public:
virtual void fun ( int x )
{
cout << "Derived::fun(), x = " << x << endl;
}
};
int main()
{
Derived d1;
Base *bp = &d1;
bp->fun(5);
return 0;
}
Because in your base class you only have a function fun(). It is only in your derived class that you have fun(int). You fun(int) is not overriding the fun() in the base class, rather it is creating a new function entirely - function signature is not only the name but the name and the parameters.
Since you are accessing the object through a base pointer, you can't access fun(int) but fun().

why this pointer value changed after adding virtual keyword in C++

Consider below line of code
#include<iostream>
using namespace std;
class base
{
int i;
public:
void printb()
{
cout << "This pointer of base "<<this<<endl;
}
};
class derived: public base
{
int i;
public:
void printd()
{
cout << "This pointer of derived "<<this<<endl;
}
};
main()
{
derived d1;
d1.printd();
d1.printb();
}
After compiling with g++(4.8.4) in 64bit ubuntu machine result is
This pointer of derived 0x7ffe74697ac0
This pointer of base 0x7ffe74697ac0
As per my understanding both base and derived this pointer will be same since we are calling with single object.
I added virtual keyword to printd() function of derived class as below
#include<iostream>
using namespace std;
class base
{
int i;
public:
void printb()
{
cout << "This pointer of base "<<this<<endl;
}
};
class derived: public base
{
int i;
public:
virtual void printd()
{
cout << "This pointer of derived "<<this<<endl;
}
};
main()
{
derived d1;
d1.printd();
d1.printb();
}
output of above code is as below
This pointer of derived 0x7ffee969b1d0
This pointer of base 0x7ffee969b1d8
Here this pointer value is different in derived and base even if calling with single object.Every time i run the program there is difference of 1byte between derived this pointer and base this pointer.
Can anybody tell why this difference in this pointer and how virtual keyword affect this pointer.
By adding the virtual keyword, you made derived polymorphic. A common implementation of runtime polymorphism is to add a pointer to the start of the object. This vptr points to a table of functions that are dynamically dispatched (commonly known as a vtable).
As such, the base sub-object, which is not polymorphic, is offset inside the derived super-object by the hidden pointer.
You see the pointer adjusted automatically, because the compiler injects code to perform this adjustment upon calling a member function. This ensures that printb will access all the (potential) members of base in the correct location.

Accessing virtual base class function from a derived class

1.In the main function below, why does d.foo(9.5) not select the Base::foo(double) method from the base class? Doesn't the derived class inherit that method?
2.What causes the compile error?
class Base {
public:
virtual void foo(int){
cout << "Base::foo(int)" << endl;
}
virtual void foo(double){
cout << "Base::foo(double)" << endl;
}
};
class Derived : public Base {
public:
virtual void foo(int){
cout << "Derived::foo(int)" << endl;
}
};
void main() {
Derived d;
Base b, *pb = &d;
d.foo(9); // selects Derived::foo(int)
d.foo(9.5); // selects Derived::foo(int)
pb->foo(9); // selects Derived::foo(int)
pb->foo(9.5); // selects Base::foo(double)
Derived * d;
d->foo(9); // compile error
}
The compilation error is because of two variables with the same name in main().
As to your problem with inherited functions not being called for an instance of your Derived (except via pointer to Base)
The standard describes the "hiding rule", which makes this happen. Essentially, member functions declared in derived classes hide inherited functions with the same name but different signature inherited from the base class. The hiding rule is independent of whether the inherited functions are virtual or not.
The common solution is to introduce all inherited functions from the base class with using Base::foo. For example,
class Base {
public:
virtual void foo(int){
cout << "Base::foo(int)" << endl;
}
virtual void foo(double){
cout << "Base::foo(double)" << endl;
}
};
class Derived : public Base {
public:
using Base::foo;
virtual void foo(int){
cout << "Derived::foo(int)" << endl;
}
};
Another solution is to remember to explicitly override all inherited versions of the function (implement the derived class version to simply call the base class version of each function). This works with older compilers that do not support a using directive like the above. The catch is that it is necessary to explicitly do this with every inherited overload, and it is easy to miss one.
In the main function below, why does d.foo(9.5) not select the Base::foo(double) method from the base class? Doesn't the derived class inherit that method?
Yes, but it's hidden by the function with the same name in the derived class. You can unhide it with a using-declaration in the derived class:
using Base::foo;
What causes the compile error?
You're trying to declare a second variable called d. Change the name to something that's not already used; and initialise it to point to a valid object, otherwise you'll have a runtime error or other undefined behaviour.
Derived * pd = &d;
pd->foo(9); // selects Derived::foo(int)
Also, main has the wrong return type. It must return int.
1) Because this is exactly how polymorphism work. If a virtual function is redefined in a derived class, this (and only this) redefined version will be called. If the function is not virtual it's vice versa: only the base class function will be called.
//Example 1: non-virtual function
class Base
{
public:
void foo()
{
std::cout << "Base";
}
}
class Derived : public Base
{
public:
void foo()
{
std::cout << "Derived";
}
}
Base * base = new Base();
base->foo()//prints "Base"
Base * derived = new Derived();
derived->foo()//prints "Base", since the function is not virtual, and the version from the base class is called
//Example 2: virtual function
class Base
{
public:
virtual void foo()
{
std::cout << "Base";
}
}
class Derived : public Base
{
public:
void foo()
{
std::cout << "Derived";
}
}
Base * base = new Base();
base->foo()//prints "Base"
Base * derived = new Derived();
derived->foo()//prints "Derived", since the function is virtual, and the redefined version from Derived class is called
2) The compile error happens because you have a conflicting declaration - two objects are called d.
Derived * d;
d->foo(9); // compile error
You don't have instantiated the object:
Derived * d = new Derived;
If you not create the object the compiler use the previous declaration of d: Derived d that is not a pointer.

Why is initialization of derived class through a base class pointer different from that through a derived class pointer?

#include <iostream>
using namespace std;
class Base {
public:
void F(){cout << "Base::F" << endl;};
virtual void G(){cout << "Base::G" << endl;};
};
class Derived : public Base {
public:
void F(){cout << "Derived::F" << endl;};
void G(){cout << "Derived::G" << endl;};
};
int main(){
Derived *pDerived = new Derived;
pDerived->F(); //F was redefined
pDerived->G(); //G was overriden
Base *pBase = new Derived;
pBase->F();
pBase->G();
}
The output of this code is:
Derived::F
Derived::G
Base::F
Derived::G
Why does not the code produce the following output?
Derived::F
Derived::G
Derived::F
Derived::G
i.e. When a derived class object is initialized through a base class pointer, why is the function definition for a non-virtual function different from that of a derived class object initialized through a derived class pointer? Shouldn't the same type of object be initialized when we call "new Derived" whether it be from a base class pointer or a derived class pointer?
The function F() is not virtual, which means that the function call will be statically dispatched to the version in the static type of the pointer/reference, rather than let it find at runtime what the dynamic type of the object really is.
You can access the same function from a pointer to Derived if you qualify which variant you are interested in:
pDerived->Base::F();