operator=
From: Vince Morgan (vinhar_at_NOSPAMoptusnet.com.au)
Date: 02/27/05
- Next message: Ulrich Eckhardt: "Re: operator="
- Previous message: Robert W Hand: "Re: lnk 1104 error"
- Next in thread: Ulrich Eckhardt: "Re: operator="
- Reply: Ulrich Eckhardt: "Re: operator="
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 27 Feb 2005 17:17:35 +1000
Hi,
I have a question with regard to operator overloading.
Specificaly the '=' operator.
Suppose I have a string class that has two '=' overloads;
1: operator= (const char *c) {//impl}
2: operator= (const CTheClass &c) {//impl}
The following copies using 1, as I would expect.
CTheClass A;
A = "A string";
If my copy target is another CTheClass object, then 2 is
invoked, as one might also expect.
However, if I should comment out, or remove 1 , then 2 is
called in both cases, which I found quite surprising.
It appears to me that a temp object is created using the copy
ctor, and then 2 is invoked to copy it.
This implies that 2 is not required, and that the compiler
_knows_ what to do if 1 is not implemented.
My first question is whether this is (ISO)behaviour, or
compiler specific. I am using VC6.
The next question would depend on the answer to the first.
If this is not compiler specific, what is the prefered solution,
to include only the one, or both? And why?
TIA
Vince Morgan
- Next message: Ulrich Eckhardt: "Re: operator="
- Previous message: Robert W Hand: "Re: lnk 1104 error"
- Next in thread: Ulrich Eckhardt: "Re: operator="
- Reply: Ulrich Eckhardt: "Re: operator="
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|