Base Copy Constructor

Started by
7 comments, last by SiCrane 13 years, 7 months ago
Derived(const Derived& rhs) : Base(rhs) {}

Derived& operator=(const Derived& rhs) { Base::operator=(rhs); }


That's the way I implemented these in a derived class that I have but, what happens if I leave them undefined and use the derived class' default copy constructor and assignment operator? Will it end up like above?
Advertisement
Does Derived have any member variables?
Yes, both derived and base have member variables that can be shallow copied.
The copy constructor would attempt to call the default base constructor if you omitted the initialiser list. The assignment operator would be empty if you omitted the call to the superclass assignment operator.
Quote:Original post by rip-off
The copy constructor would attempt to call the default base constructor if you omitted the initialiser list. The assignment operator would be empty if you omitted the call to the superclass assignment operator.


Hey, I'm aware of that but is that still the case if I don't write a copy constructor / assignment operator at all?
Quote:Original post by Jethro_T
Quote:Original post by rip-off
The copy constructor would attempt to call the default base constructor if you omitted the initialiser list. The assignment operator would be empty if you omitted the call to the superclass assignment operator.


Hey, I'm aware of that but is that still the case if I don't write a copy constructor / assignment operator at all?

It is important to understand what the compiler supplied constructors and operators do.

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Jethro_T
Yes, both derived and base have member variables that can be shallow copied.
Then the code you posted is buggy -- you're only copying the members inside Base, not the ones inside Derived too.

The compiler-generated ones should be fine.
Quote:Original post by Hodgman
Quote:Original post by Jethro_T
Yes, both derived and base have member variables that can be shallow copied.
Then the code you posted is buggy -- you're only copying the members inside Base, not the ones inside Derived too.

The compiler-generated ones should be fine.


I omitted copying the derived's members in the example. Everytime I make a post on these forums I try to strip out irrelevant code and build a minimal example. Looking back at my initial post though, I must admit my question and example was vague. Bregma's link answered my question though.
Quote:Original post by Jethro_T
Everytime I make a post on these forums I try to strip out irrelevant code and build a minimal example.

Your judgement of what is relevant code is flawed. Post real code that you have questions about.

This topic is closed to new replies.

Advertisement