Re: TRICK: Unique ID'ing
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 08:04:13 -0800
Peter Oliphant wrote:
> 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... : )
Old trick.
An even easier way - just cast the this pointer to an integer type and call
that the ID. Guaranteed unique with absolutely no overhead.
-cd
.
- Follow-Ups:
- Re: TRICK: Unique ID'ing
- From: Peter Oliphant
- Re: TRICK: Unique ID'ing
- References:
- TRICK: Unique ID'ing
- From: Peter Oliphant
- TRICK: Unique ID'ing
- Prev by Date: Re: Link error after converting from 6.0 to 2005
- Next by Date: RE: using pinvoke to fill a string
- Previous by thread: TRICK: Unique ID'ing
- Next by thread: Re: TRICK: Unique ID'ing
- Index(es):
Relevant Pages
|
Loading