P
Peter Oliphant
Sometimes it's hard to get straight when passing or storing or returning an
instance of a class whether you are still dealing with the original object
or a copy. For example, the '=' operator used on pointers to two instance of
a class can be overloaded to return the pointer to the target instance or a
pointer to a copy of the target instance. When passing an instance of a
class it can be done so the method will manipulate the instance itself
(reference) or a copy of the instance (value). Implicit copy constructing
can also confuse things. The '==' operator can be overloaded to return true
only if the two instances are the same instance, or it can return true when
the two merely have the same value (e.g., two long variables when compared
for equality will return if they have the same value, not if they are the
same variable). So it's not always obvious if one is manipulating the object
desired or a copy of the object.
To combat this, I use the following technique, which could be called 'unique
ID'ing'. Here is reduced coded to show how it works:
class MyClass
{
public:
MyClass( ) { m_Init() ; }
~MyClass( ) {}
public:
long ID( ) { return m_ID ; }
private:
void m_Init( )
{
m_ID = ++s_ID ;
}
private:
long m_ID ;
static long s_ID = 0 ;
} ;
Since 's_ID' is static it is a single variable which will be used by all
instances of the class created. The m_Init( ) (which needs to be put in
every constructor) will increment this static variable and then store it's
new value in the instance's personal copy of 'm_ID'. Thus, 'm_ID' is unique
for each instance created!
Now if you have an instance of MyClass that you suspect might be a copy of
the intended instance, just keep track of ID of the instance you want (via
ID( )) and compare it against the suspect. If it matches you know they are
the same object. If not, you know they aren't!
There is a side bonus to this. If you examine the value of 's_ID' it will
tell you exactly how many instances of MyClass have been created so far in
the application run! Thus, one can better discover situations where copies
are being created which are unintended...
There might be other ways of doing this, possibly even built into the
system, but I still think this is a cool trick... : )
[==P==]
instance of a class whether you are still dealing with the original object
or a copy. For example, the '=' operator used on pointers to two instance of
a class can be overloaded to return the pointer to the target instance or a
pointer to a copy of the target instance. When passing an instance of a
class it can be done so the method will manipulate the instance itself
(reference) or a copy of the instance (value). Implicit copy constructing
can also confuse things. The '==' operator can be overloaded to return true
only if the two instances are the same instance, or it can return true when
the two merely have the same value (e.g., two long variables when compared
for equality will return if they have the same value, not if they are the
same variable). So it's not always obvious if one is manipulating the object
desired or a copy of the object.
To combat this, I use the following technique, which could be called 'unique
ID'ing'. Here is reduced coded to show how it works:
class MyClass
{
public:
MyClass( ) { m_Init() ; }
~MyClass( ) {}
public:
long ID( ) { return m_ID ; }
private:
void m_Init( )
{
m_ID = ++s_ID ;
}
private:
long m_ID ;
static long s_ID = 0 ;
} ;
Since 's_ID' is static it is a single variable which will be used by all
instances of the class created. The m_Init( ) (which needs to be put in
every constructor) will increment this static variable and then store it's
new value in the instance's personal copy of 'm_ID'. Thus, 'm_ID' is unique
for each instance created!
Now if you have an instance of MyClass that you suspect might be a copy of
the intended instance, just keep track of ID of the instance you want (via
ID( )) and compare it against the suspect. If it matches you know they are
the same object. If not, you know they aren't!
There is a side bonus to this. If you examine the value of 's_ID' it will
tell you exactly how many instances of MyClass have been created so far in
the application run! Thus, one can better discover situations where copies
are being created which are unintended...
There might be other ways of doing this, possibly even built into the
system, but I still think this is a cool trick... : )
[==P==]