Search code examples
c++oopdynamic-castdowngrade

C++ downgrade parent pointer to child pointer used as function parameter


I want use child pointer in place of parent parameter in some function. Here is simplified code

#include <iostream>
#include <string>
using namespace std;
class Parent
{
    private:
        string value;
    public:
        void SetValue(const string& in){ value = in; }
        const string& GetValue() const { return value; }
        virtual ~Parent() {};
};
class Child : public Parent
{
};
void Func(Parent* obj)
{
    Child* cobj = dynamic_cast<Child*>(obj);
    cobj->SetValue("new value");
    delete cobj;
}
int main(int argc, char** argv)
{
    Child* cobj = new Child();
    Func(cobj);
    cout<<"value: "<<cobj->GetValue();
    return 0;
}

But it returns value:
Func does not set the value property. How can I fix the code?
UPDATE1:
But with this code:

#include <iostream>
#include <string>
using namespace std;
class Parent
{
    private:
        string value;
    public:
        void SetValue(const string& in){ value = in; }
        const string& GetValue() const { return value; }
        virtual ~Parent() {};
};
class Child : public Parent
{
    private:
        int number;
    public:
        void SetNumber(int in){ number = in; }
        int GetNumber() const { return number; }
};
void Func(Parent* obj)
{
    Child* cobj = dynamic_cast<Child*>(obj);
    cobj->SetValue("new value");
    cobj->SetNumber(100);
    delete cobj;
}
int main(int argc, char** argv)
{
    Child* cobj = new Child();
    Func(cobj);
    cout<<"value: "<<cobj->GetValue()<<endl;
    cout<<"number: "<<cobj->GetNumber()<<endl;
    return 0;
}

I get value: number: 100
Why SetNumber work but SetValue does not?


Solution

  • You delete the object and then continue to access it. This causes undefined behaviour, which means anything may happen.

    You can fix the code by removing the line delete cobj;. However you should also check that the result of the dynamic_cast is not null before dereferencing it.

    In case it is unclear, the delete expression deletes the object being pointed to. There is no such concept as "delete the pointer".