Thread: Variable Reference Question

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Variable Reference Question

    Ok, I have this operator overloaded like this:

    Code:
    Vector3 operator + (Vector3& add);
    The code works fine but my question is, if I'm returning a non reference the overloaded operator will create a new Vector3 class and than return it. Now when it returns it, does it create a replica of the temp Vector3 class and than returns that? Here is the code for the overloaded operator:

    Code:
    Vector3 Vector3::operator + (Vector3 &add)
    {
    	return Vector3(x + add.x, y + add.y, z + add.z);
    }

    Also if I derefernece something like this: *this will it create a whole new variable of this? So if I have an operator that returns Vector& and I return the dereferenced of this, will it create a whole new this and than return a reference to it?
    Last edited by Rune Hunter; 04-01-2007 at 09:11 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    if I'm returning a non reference the overloaded operator will create a new Vector3 class and than return it. Now when it returns it, does it create a replica of the temp Vector3 class and than returns that?
    Your code creates a temporary vector3 object, and then returns a copy of it. However, if I remember correctly this extra copy will probably be optimised away.

    Also if I derefernece something like this: *this will it create a whole new variable of this?
    No, though if you "return *this;" in a function that returns an object rather than a reference to an object, it will make a copy of the current object and return that.

    So if I have an operator that returns Vector& and I return the dereferenced of this, will it create a whole new this and than return a reference to it?
    No, it will return a reference to the current object.
    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
    Aug 2004
    Posts
    731
    Alright thanks! This answered all of my questions.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, you might want to be more const-correct by passing a const reference instead, and make operator+ a const member function. Another way to do it would be to have an operator+= member function, then implement operator+ as a free function in terms of operator+=.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Call by reference question
    By badtwistofate in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 02:40 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. Passing the variable as a Reference
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 02:35 PM