how to use reference for lvalue

This is a discussion on how to use reference for lvalue within the C++ Programming forums, part of the General Programming Boards category; Hello.. i'd like to make class func that i could use it with lvalue Code: class foo { int x; ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    how to use reference for lvalue

    Hello..
    i'd like to make class func that i could use it with lvalue

    Code:
    class foo
    {
      int x;
     
      public:
      
        foo();
        void set_x();
    }
    
    foo::foo()
    {
      x=0;
    }
    
    void foo::set_x()
    {
      ...
      ... //have no idea what to do 
    }
    
    
    int main(){
     foo.set_x()=1;
    
    return 0;
    }
    i know that lvalue has a connection with putting the a value inside an adrs:
    int b=0;
    int *a=&b
    a=1 -> b=1.
    but how could i do it with class

    p.s.
    thnx in advance
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Why not just pass an argument?
    Code:
    void foo::set_x(int new_x)
    {
        x = new_x;
    }
    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
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    guess you want something like this ( I think set_x() is not a good name for a getter function )
    Code:
    class foo {
      int x;
    public:
        foo();
        int & get_x();
    };
    
    foo::foo(){
      x=0;
    }
    
    int & foo::get_x(){
        return x;
    }
    
    int main(){
         foo f;
         f.get_x()=1;
        return 0;
    }
    Kurt

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    That may be a little problematic though, since the getter then cannot be const. I think a straightforward setter is better, if you need getter/setters at all.
    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

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    That may be a little problematic though, since the getter then cannot be const. I think a straightforward setter is better, if you need getter/setters at all.
    I wouldn't call it get OR set. I'd just call it x(), since it could be used either way. blah.x() = 5.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    I wouldn't call it get OR set. I'd just call it x(), since it could be used either way. blah.x() = 5.
    It would effectively still be a getter that cannot be const, plus "x()" cannot be used as x is already a member variable.
    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

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by laserlight View Post
    It would effectively still be a getter that cannot be const, plus "x()" cannot be used as x is already a member variable.
    I would agree to any name ( maybe get_x_ref() ? ). I just don't like set_x().
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    wow i didn't thout i get so many replies so fast...

    so if i undrstand cprrectly when i do :
    Code:
    class foo{
        type varibale;
        ..
        ..
        public:
           type& func();
    }
    
    type & func(){
      return varibale;
    }
    what i do is acualy return a refernce (private case of a normal pointer)to the local varibale
    so it is actually like :

    Code:
    #include <iostream>
    
    using namespace std;
    
    class foo
    {
    	int x;
    	
    	public:
    		foo();
    		int* value();
    		void print();
    };
    
    foo::foo()
    {
    x=1;
    }
    int*  foo::value()
    {
     return &x;
    }
    
    void foo::print()
    {
    	cout<<x<<endl;
    }
    
    int main()
    {
    	foo *a=new foo;
    	int *ptr = a->value();
    	a->print();
    	*ptr=2;
    	a->print();
    
    return 0;
    }
    p.s.
    if we used the same attitude in c++ like in our life we would call a refernce a fantic pointer (will never change his opnion)
    XD
    Last edited by jabka; 04-22-2007 at 12:47 PM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Since a reference cannot be changed to refer to something else (though the value of what it refers to can be changed if it is not a const reference), it is probably closer to:
    Code:
    int* const foo::value()
    {
        return &x;
    }
    Nontheless, references are not pointers. You might want to read more on references.
    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

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Having a getter return a reference or pointer completely defeats the point in having indirect access in the first place. You can't enforce invariants on setting, you can't hide the data implementation - you might as well make the member public and save yourself the syntactic inconvenience.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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