Go Back   ZeroC Forums > Help Center

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 06-21-2007
smu smu is offline
Registered User
 
Name: Shang Mu
Organization: University of Florida
Project: JointTrack
 
Join Date: Jan 2007
Posts: 7
operator== of C++ classes

in the documentation's section C++ mapping for classes, it says

operator==
operator!=
operator<
operator<=
operator>
operator>=
The comparison operators permit you to use classes as elements of STL sorted containers. Note that sort order is based on the data members of the class, as for structures (see Section 6.12).


But when I tried to use it and looked into the source code, it turns out the current implementation merely conpairs the two pointers.
My question is, is it going to be implemented in the future or was it an old feature and won't be there any more? I just happen to need this now and it seems impossible to get around the problem except to write my own member-wise comparison code.
Reply With Quote
  #2 (permalink)  
Old 06-21-2007
marc's Avatar
marc marc is offline
ZeroC Staff
 
Name: Marc Laukien
Organization: ZeroC, Inc.
Project: The Internet Communications Engine
 
Join Date: Feb 2003
Location: Florida
Posts: 1,781
You must be looking at an old version of the manual. (I think this was a bug in the manual, but it has been fixed for quite some time now.)

Have a look at http://www.zeroc.com/doc/Ice-3.2.0/manual/Cpp.7.14.html (last sub-chapter):

Quote:
As for proxy handles (see Section 6.11.3 on page 210), class handles support the comparison operators ==, !=, and <. This allows you to use class handles in STL sorted containers. Be aware that, for smart pointers, object identity is not used for the comparison, because class instances do not have identity. Instead, these operators simply compare the memory address of the classes they point to. This means that operator== returns true only if two smart pointers point at the same physical class instance:

[...]

Last edited by marc : 06-21-2007 at 05:48 PM.
Reply With Quote
  #3 (permalink)  
Old 06-21-2007
marc's Avatar
marc marc is offline
ZeroC Staff
 
Name: Marc Laukien
Organization: ZeroC, Inc.
Project: The Internet Communications Engine
 
Join Date: Feb 2003
Location: Florida
Posts: 1,781
Oops... I just noticed that we indeed still have this sentence in the manual:
Quote:
The comparison operators permit you to use classes as elements of STL sorted containers. Note that sort order is based on the data members of the class, as for structures (see Section 6.12).
That's a bug in the manual. The paragraph I cited above is the correct one. Slice classes have reference semantics, not value semantics, therefore it compares instances, not values. See also this thread for a similar discussion.
Reply With Quote
  #4 (permalink)  
Old 06-21-2007
smu smu is offline
Registered User
 
Name: Shang Mu
Organization: University of Florida
Project: JointTrack
 
Join Date: Jan 2007
Posts: 7
thanks! That clarifies a lot.

I guess you should remove the == operator that takes two references (if that's easy to do). The paragraph you referenced above only talks about class handle compison, and that's intuitive to understand given that it's name is smart POINTER. What i did is

class A {...};

int main()
{
APtr a = new A;
APtr b = new A(a);
cout<< (*a==*b); <-- this still only compares address
}
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Operator Error - NullHandleException eric.hill Help Center 3 07-19-2006 07:10 PM
C# classes semerich Comments 3 02-08-2006 09:23 AM
slice2cpp does not support copy construct and operator = damingyipai Comments 3 06-17-2005 02:43 AM
Freeze Map iterator should add operator += n -=n dragzhb Comments 4 10-18-2004 11:48 PM
About Classes transmit level Help Center 4 09-22-2004 09:21 PM


All times are GMT -4. The time now is 11:16 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2008 ZeroC, Inc.