Johnny Pauling
Johnny Pauling

Reputation: 13397

Calling specific class method after a dynamic cast - weird behavior

This is a different question than the other one I've posted, I have this piece of code:

class Base
{
public:
    Base()
    {

    };


    virtual void Method()
    {
        cout << "Base Method";
    }
};

class Derived : public Base
{
public:
    virtual void Method()
    {
        cout << "Override Method";
    }
};


class Derived2 : public Derived
{
public:

    Derived2()
    {

        cout << "Derived2 constructor";
    }
    void Method()
    {
        cout << "Override2 Method";
    }
};

int main()
{

    Base *myPointer = new Derived();

    dynamic_cast<Derived2*>(myPointer)->Derived2::Method();

    Sleep(700);

    delete myPointer;

    return 0;
}

If I write

dynamic_cast<Derived2*>(myPointer)->Method();

there's a failure (dynamic_cast returns NULL and NULL->Method() provokes an exception) and this is what I was expecting, but if I write

dynamic_cast<Derived2*>(myPointer)->Derived2::Method();

the function succeeds without even calling the Derived2 constructor. Method isn't even a static function, what is going on here?

Upvotes: 2

Views: 1066

Answers (3)

Jesse Good
Jesse Good

Reputation: 52365

When you do Derived2::Method(); you are telling the compiler exactly what function to call. This means that it will call it directly. (also, your member function does nothing and does not rely on any member variables, so it is easy to call it directly and doesn't crash because it doesn't access anything). In your first example though, because you did not explicitly tell it which function to call, it has to lookup the function, but since you are calling it on a null pointer, the program crashes.

However, in either case you are invoking undefined behavior, and what I explained above is just an implementation detail, which may differ with other implementations.

Upvotes: 2

Puppy
Puppy

Reputation: 146910

Undefined behaviour. Your code violates strict aliasing, even if your implementation was bugged and dynamic_cast didn't return NULL when it should (or if you changed it to static or reinterpret casts) and a bunch of other mean stuff by attempting to perform this functionality. What you observe as output is irrelevant.

Upvotes: -1

David Schwartz
David Schwartz

Reputation: 182753

You triggered undefined behavior by calling a member function on a NULL pointer. If you use dynamic_cast, you must either check the returned pointer for NULL before dereferencing it or 100% ensure you never cast to a type that is not the type of the object being cast or one of its parents.

Upvotes: 4

Related Questions