operator=

From: Vince Morgan (vinhar_at_NOSPAMoptusnet.com.au)
Date: 02/27/05


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



Relevant Pages

  • Re: String concatination
    ... There is not operator + in the string class. ... the C# compiler is who translates this code in a call to the Concat string's ... The C++ compiler doesn't do this magic;). ... Un saludo ...
    (microsoft.public.dotnet.languages.vc)
  • s: AT&T C++ string class std::string compatible
    ... I am on the lookout for a string class. ... that can be linked and/or compiled with that old compiler. ... "least cost" or some types of free licence would be very welcome;-) ...
    (alt.comp.lang.learn.c-cpp)
  • Re: AT&T C++ string class std::string compatible
    ... > Hi friends, ... > I am on the lookout for a string class. ... > that can be linked and/or compiled with that old compiler. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: String Functions in MF
    ... Al Dev and others list method prototypes. ... the String class is from Al Dev. ... compiler and is not done at run-time. ...
    (comp.lang.cobol)