Script class instance assignment

Started by
1 comment, last by mandrav 13 years, 9 months ago
I have this script:

class CTest{	string name;		CTest(string s) { name = s; Print("CTest::CTest() for " + name + "\n"); }	~CTest(){ Print("CTest::~CTest() for " + name + "\n"); }		void test(){ Print("CTest::test() for " + name + "\n"); }}void test(){	CTest t1("Ent1");	CTest t2("Ent2");		t1.test();	t2.test();	//	t2 = t1; // NOTE: see what happens if you uncomment this line}


When I run this it produces the following output:


CTest::CTest() for Ent1
CTest::CTest() for Ent2
CTest::test() for Ent1
CTest::test() for Ent2
CTest::~CTest() for Ent2
CTest::~CTest() for Ent1

This looks like what I 'd expect. But if I uncomment the last line in test() (the one with the "NOTE" comment), it outputs this:


CTest::CTest() for Ent1
CTest::CTest() for Ent2
CTest::test() for Ent1
CTest::test() for Ent2
CTest::~CTest() for Ent1
CTest::~CTest() for Ent1
CTest::~CTest() for Ent1

This does not look like what I 'd expect.
I 'm sure I 'm somehow misusing the language here but can someone explain to me what I 'm doing wrong?

Thanks,
Yiannis.
Advertisement
Hi mandrav,

it's been a while. So you're back playing with AngelScript. :)

What's happening is that the compiler is creating a temporary copy of the object before calling the assignment behaviour. This is obviously needless, and I'll have to fix this.

The copy is currently created by instanciating the object using the default constructor, and then copying the content with the assigment operator. This is why you do not see the third object being constructed, and only the destructor is printed.

This is a performance issue that I've been aware of for a while, but never gotten around to fix yet.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Quote:Original post by WitchLord
Hi mandrav,

it's been a while. So you're back playing with AngelScript. :)


Hi Andreas,

yes, playing again with AngelScript. Great progress, I see so far ;)

Regards,
Yiannis.

This topic is closed to new replies.

Advertisement