Thread: References in classes

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    References in classes

    Hi,

    Is it possible to have a reference in a class?
    Here is an example:

    Code:
    class MyClass {
    public:
    	MyClass();	// Constructor
    	int& y;		// Reference (line 16)
    };
    
    // Constructor
    MyClass::MyClass()
    {			// Line 21
    	y = x;		// Reference set to outside variable
    }
    However, MSVC++ 6 gives me the following error:
    (21) : error C2758: 'y' : must be initialized in constructor base/member initializer list
    (16) : see declaration of 'y'

    Using a reference in a class is useful for wrapping things.
    I could make a template class with overloaded operators for every operator that exists to simulate the functionality of using myclass.y = 50 to set x, but surely there's a way to get the reference to work?

    Thanks.
    - Tigs

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    It is possible to have a reference member within a class, but you will need to take more care in the constructor because references must be initialized upon creation. Fortunately, a constructor initializer list is perfect for this.
    Code:
    int y;
    
    class C {
      int& x;
    public:
      C();
    };
    
    C::C()
      : x(y) // Initialization instead of assignment
    {}
    The initializer list is also convenient for const members.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    references must be initialised when created, and cannot be changed. They are effectively and alias to the "real" variable.

    >>Reference set to outside variable
    Concept: Shouldn't classes only change their own data?
    Why do want to amend a global variable through the use of a referene anyway?

    Anyways, here's one way it might work for you:
    Code:
     #include <iostream>
     
     using namespace std;
     
     int   x;
     
     class MyClass
     {
       public:
         MyClass(void);
         void  ChangeIt(int);
         int   &y;
     };
     
     MyClass::MyClass(void) :
       y(x)
     {
     
     }
     
     void MyClass::ChangeIt(int num)
     {
       y = num;
     }
     
     int main(void)
     {
       x = 11;
       MyClass c;
     
       cout << "x is " << x << endl;
       c.ChangeIt(12);
       cout << "x is " << x << endl;
     }
     
     /* 
     Output:
     
     x is 11
     x is 12
     
     */
    Doh! Been beaten, but I'm going to post this anyway!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    32
    >> Concept: Shouldn't classes only change their own data?
    In the example I used a global variable just as an example. The real use I'm putting this to is using a class to wrap a complicated tree of structures. The aim is to be able to use c.y = 5; and the variable x changes to 5 (no functions involved). So in fact something at ptr->struct1.ptr2->value is modified, whilst using much easier syntax.

    If classes only modified their own data - wouldn't this limit all the cool stuff classes can do?

    Anyways, I think I have it working now, so thanks for your help
    - Tigs

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>If classes only modified their own data - wouldn't this limit all the cool stuff classes can do?
    Some would argue that the "cool stuff" is undesirable from a design standpoint. In my experience good design is a nice goal, but in reality we are forced to use what works. Try to find a happy medium so that your code is effective both in theory and at run-time.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>wouldn't this limit all the cool stuff classes can do?
    But shouldn't the data you're amending be inside a class of its own (encapsulated)? Not that I'm arguing here, you do what's best for your app.

    >>your code is effective both in theory and at run-time.
    And 2 years later when you have to go back and debug it, having not looked at it for that amount of time
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. references to function in classes
    By Chrip in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2007, 11:39 AM
  3. References vs Pointer vs copying and storing stuff in classes
    By Monkeymagic in forum C++ Programming
    Replies: 20
    Last Post: 09-01-2006, 09:40 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. References to classes
    By gustavosserra in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2003, 10:01 PM