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.