Re: help with STL needed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

The two objects in your code are both inserted in the set because the set's
key is a pointer to the class. obj1's pointer value is different to obj3's
pointer value, and so they can both go in the set (it's basically doing an
int's < operator, not the class')

As obj4 is never inserted, it can't be found as it is looking for a matching
pointer, not the class.

In the == statement you are using the * dereference, so now the class' ==
operator is being used, and obj1 is the same as obj3.

Hope this clears things up...

Chris




----- Original Message ----- 
From: "gnuml" <gnuml@xxxxxxxxxx>
To: <gcc-help@xxxxxxxxxxx>
Sent: Thursday, May 06, 2004 4:09 PM
Subject: help with STL needed


> Hi,
>
> I am trying to use the STL-'set' container, but have a few problems with
> it.
>
> I have defined a certain class 'someclass' which is abstract. In that
> class
> I have defined the <, > and == operators for it. Whenever 2 classes
> derived
> from 'someclass' have the same key, I want them to be considered equal.
> 'class101' and 'class545' are derived from 'someclass'.
> In main() I want pointers to objects of type 'someclass' to be stored in a
> 'set', which means that no 2 elements of the set can be equal (in my case:
> no two objects that pointers point to, can have the same key). However,
> when
> I try inserting 2 objects of type class545, the set successfully does so,
> when in fact it should NOT because only one class with key '545' is
> allowed.
> Also, looking for an object of class101 (obj4 in my example) fails, which
> shouldn't, because obj2 of class101 actually is in the set.
> Finally, 'manually' comparing obj1 to obj3 works out just fine, which
> leaves
> me wondering why the set-insert/find functions don't use the operators I
> defined in someclass. Am I missing something here?
>
> Thanks alot for any help!
>
> Martin
>
> #include<string>
> #include<iostream>
> #include<set>
>
> using namespace std;
>
> class someclass
> {
> public:
>         string key;
>         string data;
>
>         virtual void dosomething() = 0;
>         virtual ~someclass() = 0;
>
>         bool operator<(someclass &rhs)
>         {
>                 return key < rhs.key;
>         }
>
>         bool operator==(someclass &rhs)
>         {
>                 return key == rhs.key;
>         }
>
>         bool operator>(someclass &rhs)
>         {
>                 return key > rhs.key;
>         }
> };
>
> someclass::~someclass()
> {
> }
>
> class class101 : public someclass
> {
> public:
>         class101( string d)
>         {
>                 key = "101";
>                 data = d;
>         }
>
>         void dosomething()
>         {
>                 cout << key << " : " << data << endl;
>         }
> };
>
> class class545 : public someclass
> {
> public:
>         class545( string d)
>         {
>                 key = "545";
>                 data = d;
>         }
>
>         void dosomething()
>         {
>                 cout << key << " => " << data << endl;
>         }
> };
>
> int main()
> {
>         set<someclass*> list;
>         set<someclass*>::iterator it;
>
>         someclass *obj1 = new class545("dfdf");
>         someclass *obj2 = new class101("fdgfdhgfhgf");
>         someclass *obj3 = new class545("ds");
>
>         someclass *obj4 = new class101("gdfgfd");
>
>         list.insert(obj1);
>         list.insert(obj2);
>         list.insert(obj3);
>
>         for(it = list.begin(); it != list.end(); it++)
>         {
>                 (*it)->dosomething();
>         }
>
>         it = list.find(obj4);
>         if( it == list.end() )
>         {
>                 cout << "NOT FOUND!" << endl;
>         }else{
>                 cout << "FOUND!" << endl;
>         }
>
>
>         if( (*obj1) == (*obj3) )
>         {
>                 cout << "obj1 and obj3 are equal" << endl;
>         }
>
>         return(0);
> }
>
>


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux