Jump to content
  • Advertisement
Sign in to follow this  
mandrav

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!