Variable Reference Question

This is a discussion on Variable Reference Question within the C++ Programming forums, part of the General Programming Boards category; Ok, I have this operator overloaded like this: Code: Vector3 operator + (Vector3& add); The code works fine but my ...

  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
    21,712
    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.
    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
    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
    21,712
    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+=.
    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

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, 01:35 PM

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