shadow copy, deep copy and bitwie copy by constructor
-
what is a bitwise copy in c++ done by default constructor and what is shadow copy and deep copy ?
-
what is a bitwise copy in c++ done by default constructor and what is shadow copy and deep copy ?
Please, Let Me Google That For You[^]. BTW It is 'shallow' copy. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
what is a bitwise copy in c++ done by default constructor and what is shadow copy and deep copy ?
Bitwise copy (AKA shallow copy) copies the bits of the struct/class, including arrays. So the destruction of either may cause problems if they are not destroyed together. A deep copy contains its own definition of all data including array to avoid this negative consequence.
-
what is a bitwise copy in c++ done by default constructor and what is shadow copy and deep copy ?
aesthetic.crazy wrote:
what is a bitwise copy in c++ done by default constructor
You mean by a compiler-generated copy constructor? It is not a bitwise copy of the object. It copies each member by invoking its copy constructor.
-
what is a bitwise copy in c++ done by default constructor and what is shadow copy and deep copy ?
If your class/struct contains pointers (i.e. dynamically allocated data) a shallow/bitwise copy will copy the value of the pointer so that both classes point to the same data object. While this is great for performance issues, it means that if 1 class modifies or deletes the data, the data will be changed or invalidated in the other copy of the class (as per T2102's answer). A deep copy on the other hand will allocate a new chunk of memory and copy the contents from the memory pointed to by pointers so that the copy will have its own copy of the dynamic data to modify and delete. Just to clarify on the use of a deep copy, you must provide the copy constructor yourself like
class MyClass : public BaseClass {
MyClass(const MyClass &objOther) : BaseClass(objOther) {
//copy any memory that is allocated by a new or malloc
}
};Note the parameter
const MyClass &objOther
, you must pass the other class in as a reference (preferable) or a pointer, otherwise the copy constructor would need to be called in order to call the copy constructor resulting in infinite recursion. If the class has a base class, you also need to call its copy constructor in order to give it a chance to copy any memory that it may have dynamically allocated. Even if you know it doesn't this is still good style and will save you hassles if you add it in later on. If your class has a statically allocated array by use of the square brackets[]
in the class definition, and no dynamic pointers that need explicit copying, then you don't need a copy constructor, as the syntaxchar szMyString[20];
will allocate 20x char inside the class, so a bitwise copy will copy that. If you provide a copy constructor, only what you explicitly copy will be copied, that is anint
variable will not be copied unless you tell it to. Anything not copied should be initialised, usually by means of an initialiser list. The bitwise copy will only be provided if you don't provide a copy constructor. If you don't want a class to be copied, simply declare its copy constructor under a private tag, then just provide no code for the function body (although you still need the codeMyClass::MyClass(const MyClass &objOther) { }
) so that the function is defined.