Re: Copy Constructor Craziness

From: Jay Tilton (tiltonj_at_erols.com)
Date: 02/09/04


Date: Mon, 09 Feb 2004 06:02:16 GMT

use63net@yahoo.com (Unknown Poster) wrote:

: The next sentence is also verrrry interesting:
: "However, this does not trigger the copying behavior that a real
: ++ operator would."
:
: Horsefeathers! That is exactly what is happening in this case.
: A copy constructor is being autogenerated.

You are mistaken. There is no copy constructor involved at all.

: In the example earlier in the thread, "++" is not EXPLICITLY
: overloaded, but "+" is (and += is not). The copy constructor
: is being autogenerated in this case - it is self-evident in
: that $g and $f end up referencing different objects, while
: there was no overloading of "=".

If the overloaded '+' returns a new object, as it does in the pared-down
demonstration class I wrote in the previous article, how could you
sensibly expect an autogenerated '++' not to? This is not any kind of
evidence that a copy constructor is being used.

If your own overloaded '+' does not return a new object, it's time for
you to show the code for your overloaded operators.

: The text of Programming Perl 3E
: seems to be wrong, or at least very misleading,

The book is at least 3.5 years behind the Perl docs' revision state.
Save accusations of incorrectness for the current version.

Obsolescence issues aside, the fourth paragraph in the "The Copy
Constructor (=)" section of chapter 13 plainly says:

    The need for copying is recognized only by mutators such as ++ or

    += . . . . If the operation is autogenerated via +, . . . then no

    copying occurs, . . . .

What do you find wrong or misleading about that?

: because I don't see any justification for this autogeneration.

You're chasing phantoms here. There is no autogenerated copy
constructor.
 
: Now, for another intersesting result. When I do explicitly overload
: "++" in the class, Perl bombs out at "++g" with an error message stating:
: "... Operation '=', no method found, argument in overloaded
: package Rational ..."

Yes. If you're overloading a mutator, there needs to be a copy
constructor. If you're letting the mutator be autogenerated, a copy
constructor is irrelevant.



Relevant Pages

  • Re: still crabby about copy constuctor craziness
    ... When the copy constructor gets ... or if a mutator has been explicitly overloaded. ... newest version explicitly overloads them. ... > no such thing as a copy constructor, and no operator overloading. ...
    (comp.lang.perl.misc)
  • Re: Copy Constructor Craziness
    ... > That second part is a tricky read. ... A copy constructor is being autogenerated. ... there was no overloading of "=". ... any justification for this autogeneration. ...
    (comp.lang.perl.misc)
  • Re: IsNumber function
    ... I've never cared what type of data gets passed to it in a VBA function and have used a variant...which basically is what you did there with the overloading. ... The disadvantage of this is that widening also takes system resources and in my opinion is poor practice, since it creates code that can be hard to read or debug. ... Stefan's code also used widening (from auto-boxed primitives up to Object) and you see that folks are complaining about it. ... I also added a trick where I declared the default constructor private, thus preventing the user from accidentally calling a constructor that did nothing. ...
    (comp.lang.java.help)
  • Re: default parameters
    ... every permutation of default params in a C++ constructor, ... can someone explain the reasoning behind why it does not? ... overloading, and which is indeed what people do today, but yes, you?re ... If you do overloads, let?s say you have void f, and ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overloading new and delete in C++.
    ... > In most tutorials on overloading 'new', they usually put malloc in the ... > malloc doesn't call the constructor and free won't call the destructor.. ...
    (comp.lang.cpp)