Default assignment operator= in c++ is a shallow copy? -


just simple quick question couldn't find solid answer anywhere else. default operator= shallow copy of class' members on right hand side?

class foo { public:   int a, b, c; };  foo f1, f2; ... f1 = f2; 

would identical to:

f1.a = f2.a; f1.b = f2.b; f1.c = f2.c; 

this seems true when test need sure i'm not missing specific case.

i'd say, default operator= copy. copies each member.

the distinction between shallow copy , deep copy doesn't arise unless members being copied kind of indirection such pointer. far default operator= concerned, it's member being copied "copy" means, deep or shallow.

specifically, though, copying raw pointer copies pointer value, doesn't referand. objects containing pointer members shallow-copied default operator=.

there various efforts @ writing smart pointers perform clone operations on copying, if use everywhere in place of raw pointers default operator= perform deep copy.

if object has standard containers members, may confusing (for example) java programmer operator= "shallow copy". in java vector member reference, "shallow copy" means vector members aren't cloned: source , destination refer same underlying vector object. in c++ vector member will copied, along contents, since member actual object not reference (and vector::operator= guarantees contents copied it).

if data member vector of pointers, don't have either deep copy or shallow copy. have semi-deep copy, source , destination objects have separate vectors, corresponding vector elements each still point same, uncloned object.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -