473,508 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TRICK: Unique ID'ing

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==]

Nov 29 '05 #1
2 1385
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
Nov 29 '05 #2
> Old trick.

I figured, but old tricks are new for each individual at least once... : )

And IMHO this forum could use some more voluntarily posts of tricks and
techniques in contrast to all of the how-do-I-solve-this-problem posts. In
one case solution-posters are being pro-active, in the other
passive-aggressive. I think this forum (and others) could benefit from
both... : )
An even easier way - just cast the this pointer to an integer type and
call that the ID. Guaranteed unique with absolutely no overhead.
Cool, but this doesn't get you the added bonus of a variable which can be
used to determine if instances ar being created. For example, I can look at
's_ID', step over a routine, and if 's_ID' has increased I know instances
have been created. There are times (like this weekend for me....lol) when I
have discovered methods that were creating and manipulating copies when I
wanted to be manipulating the originals. I stepped over the 'offending'
method, and when I saw instances being created, I knew what the problem was
and where to look for them... : )

And the 'overhead' you speak of is one long per class definition, one long
per instance, one line of code, and one single-line-of-code method (ID( )).
Not exactly what I'd call 'heavy' in this day of giga-bytes and tera-bytes
of storage.

Although, granted, your solution is both elegant and sweet... : )

[==P==]

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O5**************@TK2MSFTNGP10.phx.gbl... 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

Nov 29 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2023
by: Pro Grammer | last post by:
Hello, all, I am not sure if this is the right place to ask, but could you kindly tell me how to "load" a shared object (like libx.so) into python, so that the methods in the .so can be used? That...
17
21886
by: Eternally | last post by:
Hey folks, I'm writing a simple C++ application which runs continuously. I'm running the program from the command line in Cygwin from XP, and it's cross-compatible with Linux. While running...
4
3542
by: Jay Moore | last post by:
How do I do this? I accidentally set a column as UNIQUE and I don't need it set that way. How can I set it back to not unique? TIA -Jay
13
1537
by: Jerry Camel | last post by:
I need to be able to generate unique names for files. I was considering that hash alogorithms, but if I had two files with the same name, I'd get the same hash. I am collecting and storing files...
9
5690
by: John A Grandy | last post by:
In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '....
0
774
by: Dave | last post by:
A web page of mine looks something like: .... code ... <form id="formOne" action="Process.aspx" method="post"> .... a bunch of code ... </form> .... more code ... <form id="formTwo"...
2
999
by: pmclinn | last post by:
I import a bitmap into a .net. The bitmap is made up of black and white pixels. Is there a way to test a single point on the bitmap and discover if it is in fact black or write? I basically want...
10
2604
by: laredotornado | last post by:
Hi, I have a block of HTML of this form: <div id="outer0" class="outerDiv"> <div class="innerDiv"> <table height="100%" id="table0"> <tr style="background-color:#e0e0e0; height:15px;"><td...
4
3514
by: JBiggsCC | last post by:
I have a very simple login page which takes an ID number via a HTML form GET. What is easiest way to check that ID number against an Access DB to see if it exists? I want to redirect with the...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.