Predefined class member functions and inheritance

L

lovecreatesbeauty

Predefined class member functions and inheritance


How many member functions on earth can be provided (predefined) by
standard-compliant compilers?


Scott Meyers says that there are 6: (1)default constructor, (2)copy
constructor, (3)destructor, (4)assignment operator, (5)address-of
operator (non-const), (6)address-of operator (const), in `Effective
C++, 2nd` item 45.


Bjarne Stroustrup mentioned one more: operator, (i.e. comma operator)
in `The C++ programming Language, special edition` section 11.2.2.


Derived classes won't inherit these member functions provided
(predefine) by compilers from father classes, right?


Thank you
lovecreatesbeauty
 
R

Ron Natalie

lovecreatesbeauty said:
Scott Meyers says that there are 6: (1)default constructor, (2)copy
constructor, (3)destructor, (4)assignment operator, (5)address-of
operator (non-const), (6)address-of operator (const), in `Effective
C++, 2nd` item 45.


If Scott actually says what you say, he's wrong. The only FUNCTIONS
provided are the default constructor, the copy constructor, the
destructor, and the copy-assignment operator.

It is possible to override other operators for the class (such as
ampersand and comma) but these aren't "predefined member functions"
but intrinsic operators that can be replaced.
Derived classes won't inherit these member functions provided
(predefine) by compilers from father classes, right?

Constructors and destructors aren't "inheritted". The copy-assignment
operator (as you noted) is covered up with either the compiler generated
one or a user defined one in the derived class.

As for the operators& and ,. There is no intrisicly operator function
generated when these are not-defined. If you don't define one, then the
default behavior occurs. If you define one in a base class and don't
overload it, the derived class if it can be converted to the base, will
be:

struct A {
A* operator&() { return 0; }
} a;

struct B : A { } b;

cout << &b;

will call A::eek:perator&();
 
H

Howard

Ron Natalie said:
If Scott actually says what you say, he's wrong. The only FUNCTIONS
provided are the default constructor, the copy constructor, the
destructor, and the copy-assignment operator.

Scott does not say that, at least not there. I've just re-read item 45, and
he only talks about those four functions. I don't know where the OP got the
info about those two operators from, but it's not from item 45 in Effective
C++.

-Howard
 
L

lovecreatesbeauty

The following changes were made for the eleventh printing of the book.
These changes apply to your copy of Effective C++, Second Edition only
if you have one of the first ten printings.


! 2/10/00 ic 212 A class declaring no operator& function(s) 9/10/01
cxh 213 does NOT have them implicitly declared. Rather,
245 compilers use the built-in address-of operator
246 whenever "&" is applied to an object of that
type. This behavior, in turn, is technically
not an application of a global operator&
function. Rather, it is a use of a built-in
operator.
I eliminated mention of operator& as an
automatically generated function and adjusted the
index to eliminate entries for the removed
material.


http://www.aristeia.com/BookErrata/ec++2e-errata_frames.html
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top