Topic: Default equality/inequality
Author: "Steve Burt" <steve.burt@cursor-system.com>
Date: Fri, 22 Dec 2000 11:44:33 CST Raw View
Why does C++ not generate default operator== operator!= for a Class?
It *does* generate a default copy constructor, which just does
member-by-member copy.
Surely it could equally easily do member-by-member comparison?
Is there some subtle pitfall involved with a default equality test?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Jerry Leichter <jerrold.leichter@smarts.com>
Date: Fri, 22 Dec 2000 19:49:02 GMT Raw View
| Why does C++ not generate default operator== operator!= for a Class?
| It *does* generate a default copy constructor, which just does
| member-by-member copy. Surely it could equally easily do
| member-by-member comparison?
|
| Is there some subtle pitfall involved with a default equality test?
Relying on default operations is very often a mistake. The problem is
not with those cases where they "do the right thing"; it's with those
cases where they do the subtly *wrong* thing, and you end up with code
that compiles and *seems* to work, but actually does something very
different from what you expected.
The default operations that *are* in C++ are basically there for C
compatibility: C knows how to assign struct's (effectively element-
by-element, though C doesn't have to define it that way since you can't
really tell the difference between an element-by-element assignment and
a simple bit-move of the entire struct - something that's most
definitely not the case in C++). C knows how to pass and return
struct's by value, which in C++ needs a copy constructor - so C++ has to
know how to do that, too. C knows how to create struct's using the
moral equivalent of the C++ default constructor. C knows how to destroy
struct's. So C++ has to know how to do the same things with struct's -
and since struct's and classes are, by definition, equivalent, it also
has to do the same for classes.
If C++ were being designed from scratch, one might well argue that the
default operations should only be provided for POD's: That maintains
full compatibility with C, while avoiding many proven pitfalls.
However, the existence of defaults for all classes has been in the
language from day 1, and removing it now would be out of the question.
BTW, why stop at == and !=? Once you start on this path, why not define
all the relationals using lexicographical ordering based on element-
by-element relations?
-- Jerry
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Sat, 23 Dec 2000 13:11:36 GMT Raw View
In article <91vi84$a0n$1@soap.pipex.net>, Steve Burt <steve.burt@cursor-
system.com> writes
>It *does* generate a default copy constructor, which just does
>member-by-member copy.
>Surely it could equally easily do member-by-member comparison?
>
>Is there some subtle pitfall involved with a default equality test?
I think if we had had complete freedom there would have been no compiler
generated member functions. The four that are provided are those that C
implicitly provided and were needed in C++ for compatibility.
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]