Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
C++ copy operator of TPsVar (and derived classes)
August 24, 2016
12:26, EEST
Avatar
Fransua33
Member
Members
Forum Posts: 39
Member Since:
October 27, 2014
sp_UserOfflineSmall Offline

Hi,

Just a doubt with the C++ copy operator. Is it correct to use it?

For example if I want to create the same object in a different piece of memory like this:

TPsBool tag = new TPsBool(NULL);
tag->OnChange = onChangeRoutine;
TPsBool tag2 = new TPsBool(NULL);
*tag2 = *tag;

And this piece of code results in 2 TPsBool objects with differents pointers and the same properties, methods and events. Is that right?

Best regards!!!

August 24, 2016
13:57, EEST
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 851
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

Must admit that I haven’t used C++ enough to say for sure, but I don’t think this works as expected.

The Delphi classes have the Assign() method, instead, which you can use to make a deep copy of the objects – also in C++.

August 25, 2016
17:19, EEST
Avatar
Fransua33
Member
Members
Forum Posts: 39
Member Since:
October 27, 2014
sp_UserOfflineSmall Offline

So the correct code must be:

TPsBool tag = new TPsBool(NULL);
tag->OnChange = onChangeRoutine;
TPsBool tag2 = new TPsBool(NULL);
tag2->Assign(tag);

Does Assign copies also the events configured in the source?

August 26, 2016
10:03, EEST
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 851
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

No, it only copies the “Variable Properties”. This is the full implementation:

procedure TPsVar.Assign(Source: TPersistent);
begin
if Source is TPsVar then
CopyProps([vpValue..vpOther2], TPsVar(Source))
else
inherited Assign(Source);
end;
Forum Timezone: Europe/Helsinki

Most Users Ever Online: 267

Currently Online:
14 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 103

pramanj: 86

ibrahim: 70

kapsl: 57

gjevremovic: 49

TimK: 41

Fransua33: 39

fred: 38

Rainer Versteeg: 32

Thomas Reuther: 31

Member Stats:

Guest Posters: 0

Members: 1105

Moderators: 14

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1009

Posts: 4268

Newest Members:

user1290, gregp, mariohetheringto, normagalindo47, aurelia27u, isobel41d356980, michaeldegli, gqbdolores, kez1399, jaclynmcvay358

Moderators: Jouni Aro: 851, Otso Palonen: 32, Tuomas Hiltunen: 5, janimakela: 0, Pyry: 1, Terho: 0, Petri: 0, Bjarne Boström: 545, Heikki Tahvanainen: 402, Jukka Asikainen: 1, Teppo Uimonen: 20, Markus Johansson: 19, Matti Siponen: 53, Lusetti: 0

Administrators: admin: 0