A question about complier for C++

John Love-Jensen eljay@adobe.com
Wed Jun 25 15:46:00 GMT 2008


Hi Fan Lu,

> And in my opinion, the constructor of base class (if defined) will
> always be invoked in spite of whether the derived class has its own
> constructor,

Correct.  And if the base class constructor is not explicitly identified in
the derived class's explicit constructor, the base class default constructor
will be used.

> but the copy constructor and assignment operator in base class(if
> defined) will not be invoked if the derived class has its own.
> Is it right?

Re: copy constructor...
Correct, the derived class explicit copy constructor needs to explicitly
call the base class copy constructor (assuming that is the desired
behavior).  OTHERWISE, the base class default constructor will be used if
left implicit.

Re: assignment operator...
Correct, the derived class explicit assignment operator needs to explicitly
call the base class assignment, assuming the desired behavior is that the
base class assignment operator also be called.  OTHERWISE, the base class
will have the same member variable values that it had before the assignment
operator call (assuming nothing else has changed the state of those
variables in the derived class assignment operator).

You may want to take a look into using the std::swap technique to maintain
object state integrity during the assignment operation.  (q.v. Herb Sutter
"C++ Coding Standards" on std::swap, assignment operator, and "never fail".)

Sincerely,
--Eljay



More information about the Gcc-help mailing list