help-gplusplus
[Top][All Lists]
Advanced

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

Re: help with STL


From: Ulrich Eckhardt
Subject: Re: help with STL
Date: Thu, 06 May 2004 18:34:35 +0200
User-agent: KNode/0.7.6

Martin wrote:
> 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. 

One small nitpick: your classes don't have keys but instances/objects of
your classes.

> '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. 

Since you store pointers, std::set<> uses std::less<someclass*> for
comparisons, which only does pointer comparisons. Write a proper
comparator functor, preferably one that also deals with null pointers.

In case you still have problems, know that this is not specific to GCC's
implementation so comp.lang.c++ is the better choice.

Uli

-- 
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !



reply via email to

[Prev in Thread] Current Thread [Next in Thread]