Click here to Skip to main content
15,884,388 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Set AutoRun fail under win 7 Pin
Richard MacCutchan25-Jan-11 4:47
mveRichard MacCutchan25-Jan-11 4:47 
GeneralRe: Set AutoRun fail under win 7 Pin
yu-jian25-Jan-11 6:28
yu-jian25-Jan-11 6:28 
AnswerRe: Set AutoRun fail under win 7 Pin
yu-jian25-Jan-11 6:13
yu-jian25-Jan-11 6:13 
Questionshadow copy, deep copy and bitwie copy by constructor Pin
aesthetic.crazy25-Jan-11 2:26
aesthetic.crazy25-Jan-11 2:26 
AnswerRe: shadow copy, deep copy and bitwie copy by constructor Pin
CPallini25-Jan-11 2:44
mveCPallini25-Jan-11 2:44 
AnswerRe: shadow copy, deep copy and bitwie copy by constructor Pin
T210225-Jan-11 2:59
T210225-Jan-11 2:59 
AnswerRe: shadow copy, deep copy and bitwie copy by constructor Pin
Nemanja Trifunovic25-Jan-11 5:03
Nemanja Trifunovic25-Jan-11 5:03 
AnswerRe: shadow copy, deep copy and bitwie copy by constructor Pin
Andrew Brock25-Jan-11 5:11
Andrew Brock25-Jan-11 5:11 
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 syntax char 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 an int 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 code MyClass::MyClass(const MyClass &objOther) { }) so that the function is defined.
QuestionDialogBar in SDI in MDI applcatiion Pin
Anu_Bala24-Jan-11 22:37
Anu_Bala24-Jan-11 22:37 
QuestionHow can i delete History and Cookies Directory in Internet explorer? Pin
yogish29324-Jan-11 19:18
yogish29324-Jan-11 19:18 
AnswerRe: How can i delete History and Cookies Directory in Internet explorer? Pin
KingsGambit24-Jan-11 20:18
KingsGambit24-Jan-11 20:18 
AnswerRe: How can i delete History and Cookies Directory in Internet explorer? Pin
T210224-Jan-11 21:59
T210224-Jan-11 21:59 
QuestionBreak point not hit error in VS2005 Pin
Jia10024-Jan-11 17:28
Jia10024-Jan-11 17:28 
AnswerRe: Break point not hit error in VC2005 Pin
T210224-Jan-11 17:51
T210224-Jan-11 17:51 
GeneralMessage Removed Pin
24-Jan-11 19:09
Jia10024-Jan-11 19:09 
GeneralRe: Break point not hit error in VC2005 Pin
T210224-Jan-11 19:50
T210224-Jan-11 19:50 
GeneralMessage Removed Pin
24-Jan-11 20:07
Jia10024-Jan-11 20:07 
GeneralRe: Break point not hit error in VS2005 Pin
T210224-Jan-11 22:51
T210224-Jan-11 22:51 
AnswerRe: Break point not hit error in VS2005 Pin
Stefan_Lang27-Jan-11 2:33
Stefan_Lang27-Jan-11 2:33 
GeneralMessage Removed Pin
27-Jan-11 18:00
Jia10027-Jan-11 18:00 
GeneralRe: Break point not hit error in VS2005 Pin
Stefan_Lang27-Jan-11 22:34
Stefan_Lang27-Jan-11 22:34 
GeneralMessage Removed Pin
28-Jan-11 16:49
Jia10028-Jan-11 16:49 
GeneralRe: Break point not hit error in VS2005 Pin
Stefan_Lang30-Jan-11 22:59
Stefan_Lang30-Jan-11 22:59 
QuestionHow to display a image in an Activex webpage Pin
simon alec smith24-Jan-11 9:48
simon alec smith24-Jan-11 9:48 
AnswerRe: How to display a image in an Activex webpage Pin
Richard MacCutchan24-Jan-11 10:27
mveRichard MacCutchan24-Jan-11 10:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.