Assigning value to a function with return type as ref!

This is a discussion on Assigning value to a function with return type as ref! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std ; class N { private: int num ; public: int &geta(int n) { num ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Assigning value to a function with return type as ref!

    Code:
    #include <iostream>
    using namespace std ;
    class N
    {
        private: 
            int num ;
        public: 
           
       int &geta(int n) 
        {
          num = n ;   
            
            return num ;
        }
        
        int get() { return num;} 
    };
    int main()
    {
        N n ;
        int &a = n.geta(12) ; 
    
        cout <<"First go: " << n.get() <<endl ; 
        
        a = 100;
        
        cout << "Second go: " <<  n.get() ; 
        
        n.geta(1)=19 ;
        
        cout << "Third go: " << n.get() ;
    
        
        return 0 ; 
        
    }
    Code:
     int &a = n.geta(12) ;
    i understand this piece of code. n.geta() returns a reference of member variable of object n to a
    so a <b>IS</b> num, an alias, but still num - and can be accessed like a normal variable which breaks the rule of encapsulation and access specifiers.
    as i have done with this code
    Code:
     
    a = 100
    but I don't get this!!
    Code:
    n.geta(1)=19 
    
    number 1 where i am not storing the reference of num in any reference variable.
    i was reading some stuff that n.geta() because it returns a reference can be used as a lvalue..
    but i don't get it! 
    where does the value that n.geta(1)  go to? 
     why is it that 1 which should be stored in num is overwritten by 19..or is it overwritten?
    Speaking of that
    how is it possible to initialize a value to a function as if it was a variable? 
    i keep trying to evaluate it and each time i fail: 
    
    geta(1) takes a parameter which is stored in num, so num is 1 
    it returns the value 1..but there is no reference variable in main to hold it..
    so it is floating somewhere in memory...and I just can't go on..
    what is 19? initialized to ?
    to num but how is that possible? 19 is not an argument, also .geta(1) is returning not storing...ack! can someone help!! THanks



    oh my God can someone tell me what is happening on this code?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Notice that n.geta(1) returns a reference. So, in a way, n.geta(1) is (another name for) n.num
    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
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Thanks i get it :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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