I am learning to use VS2005 to create a VC++ MFC project to read from the serial port using the 'ReadFile' function. in particular I am having problems getting the code to compile 'ReadFile' parameter 4:
pActualBytesRead = &ActualBytesRead
ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL)
-compiles, but parameter 4 ends up with 0xbaadF00d at the time of the 'ReadFile' call?
I need help in understanding the difference between copy constructor and assignment operator, and when do we use them exactly,...and with contrast of shallow and deep copy too.
I tried to understand from internet, but still I didn't get a clear picture of it.
Could someone please put some light for the same.
I mean when do we use copy constructor and when assignment operator
In the vast majority of cases, you have to do both of them. I never encountered a case in which the copy constructor would do something different than the assignment operator.
In general, you need to provide them if you have to provide a smarter "copy" mechanism (for instance your class contains a pointer which should be recreated instead of simply assigning the same address).
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: