const Point& and const Point are same

This is a discussion on const Point& and const Point are same within the C++ Programming forums, part of the General Programming Boards category; if the Prototype is Code: Point operator-=(const Point); andFunction declearation is Code: Point Point::operator-=(const Point pt){ this->x -= pt.x; this->y ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    const Point& and const Point are same

    if the Prototype is
    Code:
    Point operator-=(const Point);
    andFunction declearation is
    Code:
    Point Point::operator-=(const Point pt){
    	this->x -= pt.x;
    	this->y -= pt.y;
    	return *this;
    }
    then I can Use it in this way
    Code:
    *pt2 -= *pt1;
    -----------------
    But when
    Code:
    Point operator-=(const Point&);
    andFunction declearation is
    Code:
    Point Point::operator-=(const Point& pt){
    	this->x -= pt.x;
    	this->y -= pt.y;
    	return *this;
    }
    then ALSO I can use it in this way
    Code:
    *pt2 -= *pt1;
    WHY ??
    Isnt there any difference between that Point& ??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    The difference is that in the former there is an additional copy constructor call.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by laserlight View Post
    The difference is that in the former there is an additional copy constructor call.
    Is there anything bad on using with & or using with & ??
    Is there anything good while Calling copy constructor.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Is there anything bad on using with & or using with & ??
    In this case, no. You should do it, in fact. I would return a reference too:
    Code:
    Point& Point::operator-=(const Point& pt){
        x -= pt.x;
        y -= pt.y;
        return *this;
    }
    Is there anything good while Calling copy constructor.
    Yes, for initialising an object to be a copy of an existing object. In this case it is unneeded, so having such a call is bad.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    K thanks

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,673
    Quote Originally Posted by laserlight View Post
    The difference is that in the former there is an additional copy constructor call.
    In principle, yes, as a temporary would be created to be passed to the function (and subsequently destroyed). However, the compiler is allowed to eliminate temporaries if the only means of detecting their existance is by tracking constructor and destructor calls.

    The statement above is more accurately written as "The difference is that in the former an additional temporary object is potentially introduced, which would yield an additional call of the copy constructor and destructor."

    Sorry; I'm in the mood for nitpicking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21