Thread: difference between returning a 'int&' instead of a 'int'

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    difference between returning a 'int&' instead of a 'int'

    can someone tell me the difference, pros and cons of both the following functions.

    Code:
    class Node
    {
    	int data_;
    	Node* prev_;
    	Node* next_;
    	
    	public:
    		Node(){data_ = 5;}
    		~Node(){};
    		int getdata(){return data_;}
    };
    
    int main()
    {
    	Node obj;
    	cout << obj.getdata() << endl;
    	return 0;
    }
    Code:
    class Node
    {
    	int data_;
    	Node* prev_;
    	Node* next_;
    	
    	public:
    		Node(){data_ = 5;}
    		~Node(){};
    		int& getdata(){return data_;}
    };
    
    int main()
    {
    	Node obj;
    	cout << obj.getdata() << endl;
    	return 0;
    }
    incase you didn't see it i added a '&' in front of the getdata member function.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the '&' gives you a reference to the variable who's memory lives inside the object. If you modify it, you modify the one in the object. Not so in the other case.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "by reference" means that you get to work with the same instance. "by value" means that you get a copy of the instance. Adding the "&" just makes it by reference instead of by value.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    well put Fillyourbrain.. thnks

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Anytime! well anytime that I'm actually logged into this message board. Truthfully its been a while.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    An advantage of returning by reference is that you can cascade operations and that you can write directly to the data member (instead of using a 'set' function), e.g.
    Code:
    int main()
    {
    	Node obj1;
    	Node obj2;
    	Node obj3;
    	
            obj1.getdata() = obj2.getdata() + obj3.getdata()
    }

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This looks somewhat awkward, though. Why is it so much better than

    Code:
    obj1.setdata(obj2.getdata() + obj3.getdata());
    Anyway, container-like classes exist to store data for you. Otherwise you are free to do anything you want with its contents. So references make sense.

    However, in case of classes that have a different purpose, returning references may limit your choices later on. May-be you want to change the class implementation, and remove the variable that you previously returned by reference. Or you might want to add code that should be executed when a member is written to, but not when it is read.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It gets more sensible when you have operator[] or operator() variants of the class. For exampe, in my recent tic-tac-toe program, I have this operator:
    Code:
    CellState& CTicTacToe::operator() (size_t x, size_t y) const
    {
    	ASSERT(x < size && y < size);
    
    	return board[x + size * y];
    }
    which means that you can do this:
    Code:
    CTicTacToe board;
    
    board(1,1) = CS_X;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Is that to say that code like

    Example:
    Code:
    static int global_x = 0;
    static int global_y = 0;
    
    int &setXY(int y)
    {
      global_y = y;
      return global_x;
    }
    
    int main(void)
    {
      setXY(5) = 3;
    }
    Is not sensible? Because I think its perfectly easy to follow.

    --Truly I hope any new programmer takes me completely unseriously and never, ever considers writing anything as malformed as that.

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    From a library-user point of view? It's because you know the code.

    Let's say you program a matrix class, then your clients will use C = A + B*D instead of some function nesting...

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    C = A + B*D is not analogous to setXY(3) = 5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM