# Script class instance assignment

This topic is 2948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by WitchLordHi 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.

1. 1
2. 2
JoeJ
20
3. 3
frob
20
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632210
• Total Posts
3004815

×