Sign in to follow this  

ashamed to ask.....but have to know for sure.

This topic is 3742 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

I am ashamed to ask, but I just *need* to check, my take on C++'s default copy constructor ( T(const T &obj) and default assignment operator (i.e. operator=(const T &obj) ) was this: both: for each member of the class T, m_member, do: m_member=obj.m_member and the default assignment operator is const T&operator=(const T &obj) and returns *this. note that what I believe is the default action is very, very different from
memcpy(this, &obj, sizeof(T));

but what about the base class(es) of T, I *think* that the default copy constructor is just: for each base class T, Base, do: Base(obj) {this makes sense} and the assignment operator do: Base::operator=(obj) is this right?

Share this post


Link to post
Share on other sites
Autogenerated copy constructors use the copy constructors of base and member classes:

class foo : base1, base2, base3 {
bar m1;
baz m2;
int m3;
};

// behaves like (or at least very close to):
foo::foo( const foo& source )
: base1( static_cast< const base1& >( source ) )
, base2( static_cast< const base2& >( source ) )
, base3( static_cast< const base3& >( source ) )
, m1( source.m1 )
, m2( source.m2 )
, m3( source.m3 )
{
}


Terminology note: The above :...,...,...,...,... syntax I'm using is called an initializer list (feel free to google this for more information -- basically, it uses/invokes constructors rather than assignment operators)

Slightly different from simple assigment, but you got the general idea down at least. Your statement holds true for operator=. And yes, it's nothing like memcpy :).

Share this post


Link to post
Share on other sites
yes I know, for the copy constructor that is what I figured, and that is what I was thinking but got too lazy to write out for the constructor but what about the assignment operator? this is the one I am most wanting to check.

Share this post


Link to post
Share on other sites
The compiler generated assignment operator will call the assignment operators of all base classes (if any) and all member variables.

Share this post


Link to post
Share on other sites

This topic is 3742 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this