473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Jul 22 '05 #1
5 1866
"Peter Meier" <pe*********@ar-solutions.de> wrote...
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
Jul 22 '05 #2
Peter Meier wrote:
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.
Jul 22 '05 #3
"Jeff Schwab" <je******@comcast.net> wrote...
Peter Meier wrote:
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.


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
Jul 22 '05 #4
Victor Bazarov wrote:
"Jeff Schwab" <je******@comcast.net> wrote...
Peter Meier wrote:
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.

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


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

Jul 22 '05 #5

"Jeff Schwab" <je******@comcast.net> wrote in message
news:k5********************@comcast.com...
Victor Bazarov wrote:
"Jeff Schwab" <je******@comcast.net> wrote...
Peter Meier wrote:

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.

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.


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


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

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
5
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
8
by: pauldepstein | last post by:
The following code was written by a colleague -- to preserve confidentiality, the name of the class is changed: HisClass operator+ (const HisClass & h1, const HisClass & h2) { // some code here}...
3
by: news.aioe.org | last post by:
Is it possible to overload increment(++) and decrement(--) postfix and prefix operators for primitive datatypes such as int, char, short, etc. in global scope (vs as a class member function where...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.