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

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

## 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 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 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 on other sites
The compiler generated assignment operator will call the assignment operators of all base classes (if any) and all member variables.

##### Share on other sites
Quote:
 Original post by kRoguebut what about the assignment operator?

Quote:
 Original post by MaulingMonkeyYour statement holds true for operator=.

:)

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633740
• Total Posts
3013621
×