update object

S

slitvinov

Hello,

Is it possible to "update" an object in this sense?
I have an object of Base class and pointers to this object. Finally I
would like to replace this object by an object of Derived class and all
pointer contain the address of the new object. Here is an unsucsessful
attempt to do it.

#include <iostream>
using namespace std;

class Base {
public:
Base();
virtual void speak();
};

class Derived: public Base {
public:
Derived();
virtual void speak();
};

Base::Base() {
}

void Base::speak() {
cout << "I am an object of class Base" << '\n';
}

Derived::Derived() {
}

void Derived::speak() {
cout << "I am an object of class Derived" << '\n';
}

int main () {
Derived objD;
Base* p1 = new Base();
Base* p2 = p1;

Derived* z = new Derived; //new class

*p1 = *z; //it is assign and the result is a Base class
p1->speak();//does not work, it is still Base class
return 0;
}
 
D

Daniel T.

Hello,

Is it possible to "update" an object in this sense?
No.

I have an object of Base class and pointers to this object. Finally I
would like to replace this object by an object of Derived class and all
pointer contain the address of the new object.

The only way to do that is to change what all the pointers point to.
 
M

Marcus Kwok

Is it possible to "update" an object in this sense?
I have an object of Base class and pointers to this object. Finally I
would like to replace this object by an object of Derived class and all
pointer contain the address of the new object. Here is an unsucsessful
attempt to do it.

#include <iostream>
using namespace std;

class Base {
public:
Base();
virtual void speak();
};

class Derived: public Base {
public:
Derived();
virtual void speak();
};

Base::Base() {
}

void Base::speak() {
cout << "I am an object of class Base" << '\n';
}

Derived::Derived() {
}

void Derived::speak() {
cout << "I am an object of class Derived" << '\n';
}

int main () {
Derived objD;
Base* p1 = new Base();
Base* p2 = p1;

Derived* z = new Derived; //new class

*p1 = *z; //it is assign and the result is a Base class

What happens here is that the Derived object gets "sliced": only the
Base parts of it get assigned to the Base object pointed to by p1.

Maybe what you want instead is:

p1 = z;
p1->speak();//does not work, it is still Base class

delete p2;
delete z;

Note that you cannot do "delete p1;" here since Base does not have a
virtual destructor.
 
S

slitvinov

Marcus said:
delete p2;
delete z;

I think it would not work for me because I don't know where the
pointers are used in the program.

I have found this solution in the archive
http://tinyurl.com/emk8u
What do you think about this pattern?

Maybe there is another solution with smart pointers? Is there a
mechanism to "run" through all pointers (a list of used pointers?) and
update them?
 
D

Daniel T.

I think it would not work for me because I don't know where the
pointers are used in the program.

I have found this solution in the archive
http://tinyurl.com/emk8u
What do you think about this pattern?

It's the Basic "State Pattern" from GoF. A great pattern if you can use
it.
Maybe there is another solution with smart pointers? Is there a
mechanism to "run" through all pointers (a list of used pointers?) and
update them?

No.
 
L

Litvinov Sergey

Daniel said:
It's the Basic "State Pattern" from GoF. A great pattern if you can use
it.

Thank you. I will try to use this pattern or "pointers to pointers"
solution you mentioned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top