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

Help with overloading the == operator.

P: n/a
Hi!

I am a newbie that needs some help.

I have my own class made with a header file and defenision file.

In my main.cpp I create a 2 vectors of the object.
How can I overload the == to compare the two vectors?

short example..
-----------------------------
Myclass o1[3],o2[3];

if(o1==o2){cout << "match";}
else{cout << "no match";}
-----------------------------

How do the "bool operator==(????????" funktion look?
Can anyone help me with this?

Regards!

Aug 4 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
dc******@sandmark.se wrote:
I am a newbie that needs some help.

I have my own class made with a header file and defenision file.

In my main.cpp I create a 2 vectors of the object.
How can I overload the == to compare the two vectors?

short example..
-----------------------------
Myclass o1[3],o2[3];
Those are not _vectors_, those are *arrays*.
if(o1==o2){cout << "match";}
else{cout << "no match";}
-----------------------------

How do the "bool operator==(????????" funktion look?
Can anyone help me with this?


Nope. Declare them as 'std::vector' then you can compare them with
the vector's own operator== :
vector<Myclass> o1(3), o2(3);
...
if (o1 == o2) ...

V
Aug 4 '05 #2

P: n/a
Hi Victor!

Thank you for your reply!
Ok, sorry for my being confused, I'm very new to c++ :)

But if I want to compare my Arrays with the == overload?
Is this possible?

Trying to learn and been having problems with this for long time.

Regards,
Gustaf

Aug 4 '05 #3

P: n/a
Gustaf wrote:
Thank you for your reply!
Ok, sorry for my being confused, I'm very new to c++ :)

But if I want to compare my Arrays with the == overload?
Is this possible?


No. You are not allowed to overload operators for built-in types,
and an array (of anything) is a built-in compound type, as is
a pointer (to anything), for example.

V
Aug 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.