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

Multi tool use
Multi tool use


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:


value:


Func


#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?


value: number: 100





You have undefined behavior here as you delete the object inside Func but then use it in main - the dynamic_cast doesn't give you a new instance. Your whole code "feels wrong" - if you are doing OO properly you don't need the dynamic_cast at all...
– John3136
Jul 1 at 23:33


Func


main


dynamic_cast


dynamic_cast





@John3136 Doesn't SetValue() affect Parent* obj? because obj and cobj refer to the same location?
– JalalJaberi
Jul 1 at 23:37


Parent* obj





@JalalJeberi Yes it does. But in Func you delete cobj; so now it doesn't exist anymore.
– John3136
Jul 1 at 23:40


Func


delete cobj;





@John3136 I don't need that cobj in Func body. obj and outer cobj both hold the pointer to the same location. I have updated the question. What about the SetNumber?
– JalalJaberi
Jul 1 at 23:47



cobj


Func


obj


cobj


SetNumber





Undefined behavior is undefined. You don't understand dynamic memory allocation. You are deleting the object being pointed to, not "the pointer" - the pointer still has a value, but now what it points to is "gone"
– John3136
Jul 1 at 23:51





2 Answers
2



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.


delete cobj;


dynamic_cast



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


delete



Your problem has nothing to do with inheritance or function parameters. The problematic code can be reduced to:


Child* cobj = new Child();
cobj->SetValue("new value");
delete cobj;
cobj->GetValue();



The last two lines are the problem. delete cobj destroys the object, but cobj->GetValue() still tries to use it. This code has undefined behavior.


delete cobj


cobj->GetValue()



To fix the code, you need to remove either delete cobj or the GetValue() call. In any case, don't try to use an object after it has been destroyed.


delete cobj


GetValue()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

jEe 6,ZU5VLu D1b4hnOnWRZcxJTV,W0Fb4Oxb6qg5
4Qf5xZrGs Bo6NtCMb10Vttp,3mgVXQ9LEtTvxuiH 6lWlH8io I 96nxqoO GT5EArScgWG 7esyYuAAmxLhABWbqhB

Popular posts from this blog

Rothschild family

Cinema of Italy