Thread: Differene between reference object and value object

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by csonx_p View Post
    Oh, now you making interesting comment to me here ... So in some regard, one can refer an address variable a const pointer to some degree! The differences between

    Code:
    Address& add;
    const Address& myadd;
    you can change the members of add class (setAddress(x, y, z), etc), but with myadd, you cant?? But, also you can't change add to another Address object....
    That's right. (And of course, you can't change myadd to another address object either.) And since it's a reference, any changes made in this function are reflected back in the original wherever-we-came-from as well. (I think we're dealing with functions here, but I admit I've lost track.)

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we were using pointers, it would be Address * const add - that is, the pointer itself can not change, but the content can.

    However, _I_ still think you should use a Address member, not a reference - there is no point in using a reference here - you still need an address, and the address itself is not a free-standing object that makes any sense. So storing the address in full inside the driver is a fine thing to do.

    --
    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.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    Code:
    Address& add;
    const Address& myadd;
    you can change the members of add class (setAddress(x, y, z), etc), but with myadd, you cant?? But, also you can't change add to another Address object....
    Put in very simply terms... you can only call const functions on the myadd variable, because it's a const reference.
    A const function is, simply put, a function that promises to never change the internal state of the class*.

    And as some have mentioned, references cannot be reassigned.

    Code:
    int a = 10;
    int b = 20;
    int& x = a;
    x = 30;
    x = b; // Oops, assigns 20 (the contents of b) to a.
    x = 40; // Oops, assigns 40 to a, not b.
    *) It's actually possible to make an exception to what const functions MAY modify in a class, but let's ignore that for now.
    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.

Popular pages Recent additions subscribe to a feed