-1

i know what virtual function and polymorphism is. i am bit confused in understanding my below code.

class  base
{
        public:
        virtual void  display()
        {
                cout <<"display base";
        }

};
class derived :public base
{
        public:
        void display()
        {
                cout << "display derived";
        }
};
int main()
{
        derived *d = dynamic_cast<derived*>(new base);   // this line is giving core dump
        d->display();
return 0;
}

i am not sure why using dynamic_cast on base class is giving me segmentation fault. i should expect the output "derived display" as output. can someone please give me any leads on it and better explanation.

ks1322
  • 33,961
  • 14
  • 109
  • 164
  • 5
    I think it should be the next line `d->display();` that segfaults, because `d == nullptr`. – Lukas-T Feb 18 '21 at 09:20
  • 3
    You created only a `new base`. Why would it be castable to `derived`? It would only be castable if it had originally been created as a `derived` or some subclass thereof. But it wasn't. So it's not. – underscore_d Feb 18 '21 at 09:22
  • 4
    you got polymorphism backwards. Every `derived` is-a `base` but not every `base` is-a `derived` – 463035818_is_not_an_ai Feb 18 '21 at 09:22
  • yes i am getting segfault in d->display() – santosh kumar Feb 18 '21 at 09:23
  • 2
    `i should expect the output "derived display" as output.` why? With `new base` you construct an object of the type `base`, that object has never been of the type `derived`, so why do you think that cast should be valid? – t.niese Feb 18 '21 at 09:26
  • You probably wanted to write a cast from `derived` to `base`: `base *b = dynamic_cast(new derived);`. This cast will not return `nullptr` with subsequent segfault. – ks1322 Feb 18 '21 at 09:27
  • 1
    @ks1322 while `base *b = dynamic_cast(new derived);` would work, a `dynamic_cast` would not make sense here because you do an upcast. You use `dynamic_cast` for down and sideways casts. But those casts are only valid if the object stored in the pointer is actually of that type you cast to. – t.niese Feb 18 '21 at 09:29
  • 1
    You never create any instance of class `derived`, so what object do you want to invoke the `derived::display` method on? – David Schwartz Feb 18 '21 at 09:56

2 Answers2

2

You have it backwards. dynamic_cast is for the case where you have a pointer to Base that may be pointing to a Derived object. dynamic_cast lets you test if that is the case or not, and if it is gives you a Derived pointer to work with. But note it only does this in the case where you had a Derived object in the first place. dynamic_cast does not magically convert a Base object into a Derived object. That would be impossible in the general case.

Base* ptr = ...;
Derived* ptr2 = dynamic_cast<Derived*>(ptr);
if (ptr2)
{
    cout << "derived object\n";
    // do something with ptr2
    ...
}
else
{
    cout << "base object\n";
}
john
  • 85,011
  • 4
  • 57
  • 81
2

As said by others, you're doing something illegal here.

The polimorphism you're trying to leveraging here, it's about pointing to a derived object with the interface of it's father but when you try to interpret an instance of the ancestor with it's son's interface, you'll get an error.

If you want to check better how this technique is carried out in C++, you can have a look to cpluplus.com reference page which explains the basic. Then you can go further in details after you have a better global look at this topic.

So, practically, what you'd like to get is

    base* d = new derived;
    d->display();

which outputs

display derived

linsock
  • 190
  • 1
  • 9
  • 1
    This isn't really a good example, because `base* d = new derived` doesn't need a cast at all, much less a `dynamic_cast`. Further, the issue goes the other way around: `derived* dp = dynamic_cast(d);` would be a valid use of `dynamic_cast`. – Pete Becker Feb 18 '21 at 14:36
  • You're right, i focused more on highlight the basic mistake to address the requirement of the question than giving a real good answer. Thank you for pointing that out, I'll fix immediatly. – linsock Feb 18 '21 at 15:02