Thread: const Point& and const Point are same

  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
    28,413
    The difference is that in the former there is an additional copy constructor call.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,815
    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, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  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, 11:58 AM