gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

OffTopic > (what happens)Matrix m7 = m0 * (m1+m2) * (m3 * m4) * m5 * m6

#117352 - keldon - Sun Feb 04, 2007 2:25 am

Code:
Matrix m7 = m0 * (m1+m2) * (m3 * m4) * m5 * m6;

Let's take the given line of code and assume anything you want, so long as we all can assume that there will be no memory leaks. In a managed environment with operator overloading (such as the .net CIL) the example code would have created many instances of the Matrix class, and disposed of them. Is this how it should be handled? How many new instances would/should be created?

My answer is 2; only two should be created. However with the current languages you are going to see 6 new instances created with this line of code. In fact if these variables were only created for the creation of m7 and will never be used again then we do not need to create any new instances, we can simply use existing objects. No language allows for this, in order for this sort of feature to be able to be handled by the compiler there must be two version of the overloaded operators: a mutable version and an immutable version.

Is there an application for an object that has both mutable and immutable types of access?

Discuss!!

#117357 - tepples - Sun Feb 04, 2007 3:41 am

C++ has both const methods (which do not change *this) and non-const methods (which change *this). Compiler can guess which version is needed from context.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#117370 - keldon - Sun Feb 04, 2007 10:43 am

To be honest I was thinking more of newer languages such as C# and Java. But even in c++ if you didn't want to alter any of the instances then (m1+m2) and (m3*m4) would have to create new instances as well as m5*m6 - then making it 3 new instances [not 2]. So wouldn't you have 3 new instances created from that statement; but then again that would be fine too.

Ignore the c++ part, I was experimenting with scripting via a CIL and come across the garbage collection issue!