Jump to content

is it correct behaviour?


photo

Recommended Posts

class StringField {
string text;

StringField clone(){
	StringField res = new StringField();
	res.text = text;     // don't work

	string temp = text;		
	//res.text = temp;   // work

	log.message("clone : [%s] -> [%s]    temp:  [%s]   \n",text,res.text,temp);
	return res;
}
};

int init() {
StringField a = new StringField(); a.text = "Hello World";
StringField b = a.clone();	
return 1;
}

 

Results:

clone : [Hello World] -> [] temp: [Hello World] // original code

clone : [Hello World] -> [Hello World] temp: [Hello World] // uncommented line "res.text = temp;"

Link to comment

This is a "feature" of user class implementation.

There are two workarounds of this feature:

 

* use constructor with arguments in clone function:

StringField clone() { return new StringField(text); }

 

* use temporary variables for fields copy.

Link to comment
×
×
  • Create New...