Why define operator == global and not as a class member?

P

Peter Meier

Hello everybody,

Stroustrup says he prefer's to declare operators, which do not do anything
on the class itself global. Does anybody know the reason for that?
Any advantages/disadvantages?

Thank you, Peter
 
V

Victor Bazarov

Peter Meier said:
Stroustrup says he prefer's to declare operators, which do not do anything
on the class itself global. Does anybody know the reason for that?
Any advantages/disadvantages?

Advantage is the possibility to use the implicit conversions that
are often defined by the class itself.

Example 1 (member):

class A
{
int i;
public:
A(int ii) : i(ii) {}
bool operator ==(A const& a) const { return i == a.i; }
};

int main()
{
A a1(42), a2(73);
a1 == a2; // works fine
a1 == 12; // works fine, '12' is converted into a temporary A
666 == a2; // ERROR doesn't work, '666' cannot be converted
}

Example 2 (non-member)

class A
{
int i;
public:
A(int ii) : i(ii) {}
int get() const { return i; }
};

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

int main()
{
A a1(42), a2(73);
a1 == a2; // works fine
a1 == 12; // works fine, '12' is converted into a temporary A
666 == a2; // works fine, '666' is converted into a temporary A
}

I am surprised that you didn't find Dr.Stroustrup's explanation as to
why non-members are better.

Victor
 
J

Jeff Schwab

Peter said:
Hello everybody,

Stroustrup says he prefer's to declare operators, which do not do anything
on the class itself global. Does anybody know the reason for that?
Any advantages/disadvantages?

Thank you, Peter

It makes the syntax similar for built-in and user-defined types.
Similar syntax makes generic programming simpler.


Also, IMO, it's a good idea to limit the number of class methods as much
as possible, so that it's relatively easy to monitor accesses to the
class data.
 
V

Victor Bazarov

Jeff Schwab said:
It makes the syntax similar for built-in and user-defined types.

Huh?

struct A {
bool operator==(A const&) const { return false; }
};

int main() {
A a1, a2;
a1 == a2; // Different ???
}
Similar syntax makes generic programming simpler.

So, what's the advantage of making it non-member?
Also, IMO, it's a good idea to limit the number of class methods as much
as possible, so that it's relatively easy to monitor accesses to the
class data.

Who says that the members have to access class data directly? If you
are so inclined to monitor the access, use accessors and monitor.

Victor
 
J

Jeff Schwab

Victor said:
Huh?

struct A {
bool operator==(A const&) const { return false; }
};

int main() {
A a1, a2;
a1 == a2; // Different ???
}

You're right, for operators the syntax is identical.
Who says that the members have to access class data directly? If you
are so inclined to monitor the access, use accessors and monitor.

....or just don't make things members if they don't need to be.
 
N

Nick Hounsome

Jeff Schwab said:
You're right, for operators the syntax is identical.

Perhaps you were thinking of the symmetry issue when you want to compare
class to a
builtin- type:
You can write an operator for A() == 42 either as a member or non-member but
42 == A() can only be done with
a non-member.
The usual method is:
struct A
{
A(int x); // conversion from int
int compare(const A&) const; // return positive neagtive or 0 for >,==,<
};
inline bool operator==(const A& a,const A& b) { return a.compare(b) == 0; }

This handles both the cases above by converting the int to an A;

The use of the compare member means that all comparisions are done in one
method and the usual operators
can be trivivially defined as inline non-members that do not need friend
access e.g.

inline bool operator<=(const A& a,const A& b) { return a.compare(b) <= 0; }
etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top