Thread: References

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by High Voltage View Post
    hope I understood you laserlight well, this is how I see it now
    https://s1.postimg.org/94pu6wotb/Untitled.pngimage hosting 5mb
    *this is not 1000. this is a pointer to the class itself, hence *this is the class itself. The class is not an integer, hence that is not valid.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by Elysia View Post
    *this is not 1000. this is a pointer to the class itself, hence *this is the class itself. The class is not an integer, hence that is not valid.
    *this gives us object, but object is just a memory address, as it is any variable in C++, that is what I actually meant, am I right?

    Quote Originally Posted by MutantJohn View Post
    I don't ever recommend returning by reference. Especially when it looks like you're receiving a parameter by value and then returning a reference to that. In all likelihood, that's UB. Just return by value.
    I am aware of that, just trying to understand references

    Quote Originally Posted by GReaper View Post
    You're over-complicating it, in my opinion. Think of it like this: A pointer is the position of a variable in memory. A reference (in C++ terms) is the variable itself. That's how I view it, and it works pretty well.

    Your logic makes sense.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by High Voltage View Post
    *this gives us object, but object is just a memory address, as it is any variable in C++, that is what I actually meant, am I right?
    No, a reference is not a memory address. It's a type that may contain data and functions. Hence it does make sense to treat it as an "int".

    If you're just trying to understand references, then just think of them as aliases. They share storage with the variable they reference. In essence, they're the same variables, but with different names. You don't need to complicate it any further.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by Elysia View Post
    No, a reference is not a memory address. It's a type that may contain data and functions. Hence it does make sense to treat it as an "int".

    If you're just trying to understand references, then just think of them as aliases. They share storage with the variable they reference. In essence, they're the same variables, but with different names. You don't need to complicate it any further.
    I understood that references can be used as aliases

    Code:
    class Foo
    {
        private:
            int x;
    
        public:
            Foo() : x(0) {}
            int getX() const {return x;}
            void setX(int _x) { x = _x;} 
    };
    
    int main(void)
    {
        Foo foo;
        Foo& fooRef = foo;
         
        printf("fooRef: %d\n", fooRef);
        printf("foo: %d\n", foo);
        
        cout << "foo.getX(): " << foo.getX() << endl;
        cout << "fooRef.getX(): " << foo.getX() << endl << endl;
        
        foo.setX(999);
        cout << "After calling foo.setX(999);" << endl;
        cout << "foo.getX(): " << foo.getX() << endl;
        cout << "fooRef.getX(): " << foo.getX() << endl;
    }
    code produces following

    fooRef: 0
    foo: 0
    foo.getX(): 0
    fooRef.getX(): 0

    After calling foo.setX(999);
    foo.getX(): 999
    fooRef.getX(): 999

    But I think references might share different storage like in this case:

    Code:
    class Test
    {
        int &i;   // int *const i;
        int &j;   // int *const j;
        int &k;   // int *const k; 
    };
    
    int main()
    {    
        // This will print 12 i.e. size of 3 pointers
        cout<<  "size of class Test = "  <<   sizeof(class Test)  <<endl;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. References
    By Dae in forum C++ Programming
    Replies: 7
    Last Post: 07-23-2009, 03:29 AM
  2. C references
    By Akkernight in forum C Programming
    Replies: 2
    Last Post: 02-27-2009, 07:53 PM
  3. References
    By Lurker in forum C++ Programming
    Replies: 7
    Last Post: 12-23-2003, 12:01 PM
  4. Help via references please!
    By Matt13 in forum C Programming
    Replies: 5
    Last Post: 11-11-2003, 08:09 AM
  5. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 08:14 AM

Tags for this Thread