class reference members

This is a discussion on class reference members within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm learning C++ and I encounter the use of reference in class. I wrote this code to look at ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    454

    class reference members

    Hi,
    I'm learning C++ and I encounter the use of reference in class. I wrote this code to look at a reference member of a class:
    Code:
    #include <iostream>
    
    class Ref
    {
    private:
      int & x;
    public:
      Ref(int & a);
      ~Ref();
      void show() {std::cout<< x << std::endl;};
    };
    
    Ref::Ref( int & a): x(a)
    { }
    Ref::~Ref()
    {}
    
    
    int main()
    {
      int z = 4;
      Ref y = Ref(z);
      y.show();
      z = 5;
      y.show();
      return 0;
    }
    In this code, x is a private reference member of class Ref. Then, I initialize the variable z to 4. then the Ref y variable is initialize with the constructor that take in the reference to z. As I change the value of 'z', I also change the value of the the private reference member. My questions are:
    1. Why doesn't the compiler protest when I try to change a private member from outside the class? I initially believed that private member can't be changed unless by a member functions of a friend.
    2. Isn't it redundant to declare a reference private member? Wouldn't it serve the same purpose to use a public member instead?
    I appreciate all the inputs.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    >> 1. Why doesn't the compiler protest when I try to change a private member from outside the class?
    Because C++ compilers do not do that kind of static analysis. It really is that simple.

    >> 2. Isn't it redundant to declare a reference private member?
    I don't know about redundant, but I've never used one. You've clearly demonstrated that it breaks encapsulation so maybe you'll stay away from it for that reason too?

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I've only seen reference members used a few times and each time they confused me to no end. Something about seeing the ampersand in a class declaration just makes my brain go awry. There are reasons for doing it but I do not remember what they are.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Being private means that you cannot access Ref::x. It doesn't mean that the class controls what you can do with other references you might have to its members.

    If you have access to the referenced variable (z), you can change it, if you only have an instance of Ref, you can't.

    Consider this:
    Code:
    #include <iostream>
    
    class Ref
    {
    private:
      int & x;
    public:
      Ref(int & a);
      ~Ref();
      void show() {std::cout<< x << std::endl;};
    };
    
    Ref::Ref( int & a): x(a)
    { }
    Ref::~Ref()
    {}
    
    void show(const Ref& ref)
    {
      //in this function you have no access to the reference
      ref.show();
    }
    
    int main()
    {
      int z = 4;
      Ref y = Ref(z);
      show(y);
      z = 5;
      show(y);
      return 0;
    }
    In principle it is quite similar to having const and nonconst references to a value. If you have a const reference to something, it means you can't use this reference to modify the value. If you also have nonconstant references to the same value, you can modify it using those.

    Code:
    #include <iostream>
    
    int main()
    {
        int z = 10;
        const int& ref = z;
        std::cout << ref << '\n';
        //ref = 20; //not allowed
        z = 20; //fine
        std::cout << ref << '\n';   
    }
    I've only seen reference members used a few times and each time they confused me to no end. Something about seeing the ampersand in a class declaration just makes my brain go awry. There are reasons for doing it but I do not remember what they are.
    A reference member means that by default the class will be copyable but not assignable, which is a rather unusual combination. If both are wanted, a pointer member allows that.
    Last edited by anon; 11-24-2010 at 04:34 AM.
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing members of parent class
    By Snip in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2006, 01:28 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Class with members as classes
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 05:58 PM
  5. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 09:56 AM

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