473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

update object

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;
}

Jun 7 '06 #1
5 1617
In article <11**********************@u72g2000cwu.googlegroups .com>,
"sl*******@gmail.com" <sl*******@gmail.com> wrote:
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.
Jun 7 '06 #2
sl*******@gmail.com <sl*******@gmail.com> wrote:
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.
return 0;
}


--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 7 '06 #3
Marcus Kwok wrote:
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?

Jun 7 '06 #4
In article <11**********************@h76g2000cwa.googlegroups .com>,
"sl*******@gmail.com" <sl*******@gmail.com> wrote:
Marcus Kwok wrote:
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?


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.
Jun 7 '06 #5
Daniel T. wrote:

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.

Jun 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ray | last post by:
I am having my first experience using BLOB as a row in a table. I am using it to insert graphics for labels we print. I have no problem inserting into and select from the table. The graphic is...
4
by: Jonathan Upright | last post by:
Greetings to anyone who can help: I'm using WebMatrix to make ASP.NET pages, and I chose the "Editable DataGrid" at the project selector screen. As you may know, it defaults to the Microsoft...
15
by: graham | last post by:
Hi all, <bitching and moaning section> I am asking for any help I can get here... I am at the end of my tether... I don;t consider myself a genius in any way whatsoever, but I do believe I have...
5
by: Louis LeBlanc | last post by:
Hey folks. I'm new to the list, and not quite what you'd call a DB Guru, so please be patient with me. I'm afraid the lead up here is a bit verbose . . . I am working on an application that...
0
by: Jim in Arizona | last post by:
I'm experimenting by following instructions at this address: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/datalist.aspx on how to use the DataList. I was trying out the...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
0
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a gridview that binds to an object datasource. I added an update method to the object data source through the wizard. The update method takes an object as input and returns an object of...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.