|
ULARGE_INTEGER currPos;
LARGE_INTEGER offset;
offset.QuadPart = 0;
istreamInterfacePointer->Seek(offset, STREAM_SEEK_END, &currPos);
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi to All,
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.
Thanks in advance.
-----------------------------
I am a beginner
|
|
|
|
|
MyClass a;
MyClass b(a);
MyClass c = b;
MyClass d;
d = c;
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
thanks,
But when do we overload each of them?...I mean when do we use copy constructor and when assignment operator
-----------------------------
I am a beginner
|
|
|
|
|
hrishiS wrote: 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).
|
|
|
|
|
thanks a lot,
Could you please elaborate little more on the point.
Cedric Moonen wrote: smarter "copy" mechanism (for instance your class contains a pointer which should be recreated instead of simply assigning the same address).
May be if you dont mind one example...
Regards
-----------------------------
I am a beginner
|
|
|
|
|
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.
|
|
|
|
|
Here[^] is a good article which takes assignment operators in detail. Its worth reading.
|
|
|
|
|
thanks you for your reply.I will go through it
-----------------------------
I am a beginner
|
|
|
|
|
Wow, that's the best article I've seen on C++ copy constructors and assignment operators. Thanks!
|
|
|
|
|
Its very similar to initialization and assignment.
int i = 10; is initialization.
This is where copy constructor works in case of classes.
int i;<br />
i = 10;
This is assignment. This is where assignment operator works in case of classes.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Ok, I understood that..thanks
But could you please differentiate these with respect to shallow copy and deep copy?...and why we say so?
-----------------------------
I am a beginner
|
|
|
|
|
|
hrishiS wrote: But could you please differentiate these with respect to shallow copy and deep copy?...and why we say so?
See here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi,
In the below code ret value become invalid as it comes out of the funcation scope. can any suggest better idea to solve this.
CString ToStr()
{
CString ret = "test";
return ret;
}
void fun()
{
CString val = ToStr(); // it returns nothing (bad pointer)
}
I have compiled this code in MS VC++2005 under winxp OS.
Thanks,
Nandu
|
|
|
|
|
void ToStr(CString& val)
{
val = "test";
}
void fun()
{
CString val;
ToStr(val);
}
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr().
Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr
-----------------------------
I am a beginner
|
|
|
|
|
hrishiS wrote: the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr().
A copy of the object will be returned, so no there's no problem there.
hrishiS wrote: Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr
1) global variable for that is really ugly (but that's my personal taste)
2) variable of type pointer: not really nice neither because each time you will create the object on the stack and furthermore, you delegate the destruction of the pointer to somewhere else, which potentially introduce memory leaks.
3) return a reference to the object: that's wrong. In this case, the object will be destroyed, no copy will be made and you will return the address of the destroyed object which is wrong.
So in conclusion, returning the object as he mentioned is the best option.
I could agree with superman in the sense that his proposed solution is more efficient: no temporary object gets created but honestly, it doesn't make a lot of difference (unless your code is in a part of the code which should be highly optimized).
|
|
|
|
|
thanks for all the points... I can understand How much I have to learn more in deep ...And sorry to the real sender of the question... pls listen/take advice from the the seniors...I am too a learner
-----------------------------
I am a beginner
|
|
|
|
|
There's no problem with your code. The problem appears when you return something like this:
char* ToStr()
{
return "test";
}
In which case you return the address of a temporary variable. If you return an object, the compiler will make a copy of it and assign it to val correctly.
EDIT: changed the return type to char* instead of CString.
modified on Tuesday, September 8, 2009 9:54 AM
|
|
|
|
|
You sure about that Cedric?
I think you'll find that "test" is used to construct a CString object that's returned by copy...that's what happened when I compiled and debugged it, anyway
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Ooops, sorry,I forgot to change the return type to char*
If the return type is CString, then I agree with you, there's no problem neither.
Thanks for pointing it out.
|
|
|
|
|
I don't find any issues with your code. You are returning by value and not returning any reference to the temporary. So I guess your code is perfectly valid.
Cedric Moonen wrote: If you return an object, the compiler will make a copy of it and assign it to val correctly.
Even if you return by value, with return value optimization, a copy won't be made.
Please correct me if I am wrong here.
[edit]Saw your post updated[/edit]
|
|
|
|
|
Your example isn't be a problem since "test" is in the data segment and isn't going to change location. I would recommend returning a const char * , but that code is perfectly fine. What isn't fine is the following:
char* ToStr()
{
char buffer[5];
lstrcpy(buffer, "test");
return buffer;
}
|
|
|
|
|
If CString is the standard ATL/MFC string class, then there is nothing wrong with the code you've posted - ret will effectively be copied into val when the return statement is executed. Part of the implied contract of ToStr is that callers will pass the address of a CString object into which the return value can be copied.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|