Today in university I was recommended by a professor that I'd check for (this != ©)
in the copy constructor, similarly to how you should do it when overloading operator=
. However I questioned that because I can't think of any situation where this
would ever be equal to the argument when constructing an object.
He admitted that I made a good point. So, my question is, does it make sense to perform this checking, or is it impossible that this would screw up?
Edit: I guess I was right, but I'll just leave this open for a while. Maybe someone's coming up with some crazy cryptic c++ magic.
Edit2: Test a(a)
compiles on MinGW, but not MSVS10. Test a = a
compiles on both, so I assume gcc will behave somewhat similar. Unfortunately, VS does not show a debug message with "Variable a used without being initialized". It does however properly show this message for int i = i
. Could this actually be considered a c++ language flaw?
class Test
{
Test(const Test ©)
{
if (this != ©) // <-- this line: yay or nay?
{
}
}
Test &operator=(const Test &rhd)
{
if (this != &rhd) // <-- in this case, it makes sense
{
}
}
};
I agree that self check doesn't make any sense in copy constructor since object isn't yet created but your professor is right about adding the check just to avoid any further issue. I tried with/without self check and got unexpected result when no self check and runtime error if self check exists.
When created copy constructor and called member function i didn
Output:
Inside default constructor
Inside parameterized constructor
Inside copy constructor
Constructor is valid
This may be correct syntactically but doesn't look right. With self check I got runtime error pointing the error in code so to avoid such situation.
Runtime Error:
Error in `/home/bot/1eb372c9a09bb3f6c19a27e8de801811': munmap_chunk(): invalid pointer: 0x0000000000400dc0
If you want to be paranoid, then:
You never want to continue if
this == ©
. I've never bothered with this check. The error doesn't seem to frequently occur in the code I work with. However if your experience is different then the assert may well be worth it.Your instructor may be thinking of the check for self-assignment in the copy assignment operator.
Checking for self-assignment in the assignment operator is recommended, in both Sutter and Alexandrescu's "C++ Coding Standards," and Scott Meyer's earlier "Effective C++."