If I have this
and I do
then the copy constructor is called and a shallow copy is made. Is a.B copied as well?
I know that a shallow copy in languages like C# or Java don't copy the objects, since they only have references. But what about in C++?
While true, I think that's a bit incomplete.
Originally Posted by tabstop
Each member is "copied," but what that actually means depends on the type of member.
Objects held by value are copied, meaning a new instance is created using the copy constructor.
Pointers are copied, meaning a new pointer is created but the underlying object is not altered or copied.
References are copied, meaning a new reference is created but again, the underlying object is not altered or copied.
Generally, if the member objects themselves do "the right thing" when copied, then a class containing these objects will also do the right thing.
A shallow copy has the same meaning in all languages. The question here is whether this is a shallow copy or not. The answer to that depends on the behaviour of AnotherObj. I.e. whether it is ref-counted, CoW, or just plain duplicated in the copy-constructor etc.
Originally Posted by C_ntua
If AnotherObj's only member were an int for example, then here a shallow copy and a deep copy are the same thing.