By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

comparing objects using pointers

P: n/a
Is the following ok ?
class Color {

private:
Color() {}

public:

static Color red;
static Color green;
static Color blue;
static Color orange;

};

bool operator==(const A& a1, const A& a2)
{
return &a1 == &a2;
}


Basically, I create some global objects (a new Color object can not be
instanciated) and since red, green, blue, orange are distinct, I think
they can be compared using their address.

Is that right ?
Apr 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"jalina" <ja****@nospam.please.com> wrote in message
news:44***********************@news.wanadoo.fr...
: Is the following ok ?
:
:
: class Color {
:
: private:
: Color() {}
:
: public:
:
: static Color red;
: static Color green;
: static Color blue;
: static Color orange;
:
: };
:
: bool operator==(const A& a1, const A& a2)
you mean: Color& a1, const Color& a2 );
: {
: return &a1 == &a2;
: }
:
:
:
:
: Basically, I create some global objects (a new Color object can not be
: instanciated) and since red, green, blue, orange are distinct, I think
: they can be compared using their address.
:
: Is that right ?

It will work portably according to the C++ standard: every object
instance is required to have a unique address for the duration
of its lifetime.

This said, if the set of colors is defined at compile time,
wouldn't it be easier to use enum values ?
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Apr 23 '06 #2

P: n/a

jalina wrote:
Is the following ok ?
class Color {

private:
Color() {}

public:

static Color red;
static Color green;
static Color blue;
static Color orange;

};

bool operator==(const A& a1, const A& a2)
{
return &a1 == &a2;
}


Basically, I create some global objects (a new Color object can not be
instanciated) and since red, green, blue, orange are distinct, I think
they can be compared using their address.

Is that right ?

Yes. But it really depends on what you mean by '=='.

Apr 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.