Jump to content
  • Advertisement
Sign in to follow this  
Jethro_T

Base Copy Constructor

This topic is 2862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!