Click here to Skip to main content
15,895,746 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Get current position of pointer using IStream interface Pin
MrKBA9-Sep-09 0:19
MrKBA9-Sep-09 0:19 
GeneralRe: Get current position of pointer using IStream interface Pin
Stuart Dootson9-Sep-09 3:20
professionalStuart Dootson9-Sep-09 3:20 
QuestionSome C++ Concepts Pin
hrishiS8-Sep-09 1:07
hrishiS8-Sep-09 1:07 
AnswerRe: Some C++ Concepts Pin
«_Superman_»8-Sep-09 1:12
professional«_Superman_»8-Sep-09 1:12 
GeneralRe: Some C++ Concepts Pin
hrishiS8-Sep-09 1:16
hrishiS8-Sep-09 1:16 
GeneralRe: Some C++ Concepts Pin
Cedric Moonen8-Sep-09 1:21
Cedric Moonen8-Sep-09 1:21 
GeneralRe: Some C++ Concepts Pin
hrishiS8-Sep-09 1:27
hrishiS8-Sep-09 1:27 
GeneralRe: Some C++ Concepts Pin
Cedric Moonen8-Sep-09 1:35
Cedric Moonen8-Sep-09 1:35 
If you didn't supply a copy constructor and assignment operator, the compiler provides a default for them. These default methods simply copy the "memory area" of one object to the other. So, if your object contains a pointer, the address of the pointer will be copied. Which means that you will end up with two instances of the class, each of them having a pointer pointing to the same address. In most of the case, you don't want such a scenario because then if one instance deletes the pointer, the other one can't access it anymore (the memory to which it points has been freed).
What you want to typically do in such scenario is create another pointer and copy the pointed data. This way, the two instances contain a different pointer and each of them can safely delete it.

This was of course just an example when it makes sense to provide a copy constructor and assignment operator, but you could replace the pointer stuff by something different (for instance a resource).

Typical classes in which you want to provide a copy constructor and assignment operator are string management classes (std::string, CString, ...) because they internally create a buffer to store the data. Take the following example:

string var1 = "test";
string var2 = var1;
var2.replace(....);


In such a scenario, you absolutely don't want that the internal buffer of var1 gets modified. So, when you copy the string, you have to recreate another independent buffer.

Hope this helps.

Cédric Moonen
Software developer

Charting control [v2.0]
OpenGL game tutorial in C++

GeneralRe: Some C++ Concepts Pin
N a v a n e e t h8-Sep-09 1:28
N a v a n e e t h8-Sep-09 1:28 
GeneralRe: Some C++ Concepts Pin
hrishiS8-Sep-09 1:31
hrishiS8-Sep-09 1:31 
GeneralRe: Some C++ Concepts Pin
Alan Balkany8-Sep-09 5:48
Alan Balkany8-Sep-09 5:48 
GeneralRe: Some C++ Concepts Pin
«_Superman_»8-Sep-09 1:30
professional«_Superman_»8-Sep-09 1:30 
GeneralRe: Some C++ Concepts Pin
hrishiS8-Sep-09 1:33
hrishiS8-Sep-09 1:33 
GeneralRe: Some C++ Concepts Pin
Selvam R8-Sep-09 2:10
professionalSelvam R8-Sep-09 2:10 
GeneralRe: Some C++ Concepts Pin
David Crow8-Sep-09 3:08
David Crow8-Sep-09 3:08 
QuestionFunction out of scope Pin
Nandu_77b8-Sep-09 0:54
Nandu_77b8-Sep-09 0:54 
AnswerRe: Function out of scope Pin
«_Superman_»8-Sep-09 1:05
professional«_Superman_»8-Sep-09 1:05 
AnswerRe: Function out of scope Pin
hrishiS8-Sep-09 1:13
hrishiS8-Sep-09 1:13 
GeneralRe: Function out of scope Pin
Cedric Moonen8-Sep-09 1:46
Cedric Moonen8-Sep-09 1:46 
GeneralRe: Function out of scope Pin
hrishiS8-Sep-09 2:09
hrishiS8-Sep-09 2:09 
AnswerRe: Function out of scope [modified] Pin
Cedric Moonen8-Sep-09 1:17
Cedric Moonen8-Sep-09 1:17 
GeneralRe: Function out of scope Pin
Stuart Dootson8-Sep-09 3:52
professionalStuart Dootson8-Sep-09 3:52 
GeneralRe: Function out of scope Pin
Cedric Moonen8-Sep-09 3:55
Cedric Moonen8-Sep-09 3:55 
GeneralRe: Function out of scope Pin
N a v a n e e t h8-Sep-09 4:22
N a v a n e e t h8-Sep-09 4:22 
GeneralRe: Function out of scope Pin
Joe Woodbury8-Sep-09 5:29
professionalJoe Woodbury8-Sep-09 5:29 

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.