Jump to content

[SOLVED] Missed information on user class clone method


photo

Recommended Posts

Posted

Hi,

In the attached sample it's defined simple A class with two attributes (p_name and p_s1). A clone method is declared to create a new A instance based on current A instance information.

The problem is the copy of p_s1 it's not created for the cloned instance. If you change the code to assign the value in a variable, and the cloned instance references this variable the code works ok.

 

clone_00.cpp

#include <samples/systems/common/systems.h>


//////////////////////////////////////////////////////////////////////////
class A {
private:
	string p_name;
	string p_s1;


public:
	A(string name, string s1= "") {
		p_name= name;
		p_s1= s1;
	}

	A clone() {
		A a= new A(p_name);
		//string s= this.p_s1;
		a.p_s1= this.p_s1;
		log.message("[%s] p_name:'%s' p_s1='%s'", __FUNC__, p_name, p_s1);

		return a;
	}

	string getS1() { return p_s1; }
};

//////////////////////////////////////////////////////////////////////////
A global_a;
A global_cloned_a;

/*
 */
void update_scene() {
	
	while(1) {
		engine.message("a:'%s' cloned_a:'%s'", global_a.getS1(), global_cloned_a.getS1());
		wait;
	}
}

/*
 */
void create_scene() {
	
	global_a= new A("global a", "this is S1"); 
	global_cloned_a= global_a.clone();
	
	return "Copy string problem on clone.";
}

Regards,

Iván.

clone_00.tar

Posted

Hi ivan. In unigine script all variables are static and all private variables are available from all class methods. You are trying to assign value to "p_s1" private variable in clone() method. But this operation "a.p_s1 = this.p_s1" equals "this.p_s1 = this.p_s1". You need to create a setter function for a "p_s1" private variable or send value to clone instance through constructor.

Posted
Thanks for the answer,

You must recognize it's a quite confusing situation. Is this language feature documented someplace?

×
×
  • Create New...