Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ Question : Reference Member Variables

Status
Not open for further replies.

matman13

Programmer
Feb 18, 2002
12
US
I've been programming in C++ for a few years now. I'm not intimate with the standard per se, but I understand, for the most part, how things work.

I've hit a problem that simply has me perplexed.

Create the following class.

class Test {
public:
int _i;
double &_d;

Test(int i, double d) : _i(i), _d(d) { }
};

Now, inside main(), do the following:

void main()
{
double d1(3.0), d2(4.0);
Test t1(3, d1);
Test t2(4, d2);

t2._d = 5.0;
}

When I run this simple program in debug mode, an interesting this occurs. After t2 is instantiated, the "_d" member variable of both t1 AND t2 is equal to 4.0. After executing the direct assignment to "t2._d", the "_d" member variable of both t1 AND t2 is equal to 5.0. Why!!!!?

I'm using Visual C++.NET to run this, but I've seen the same result on a old copy of Visual C++ 6.0. Is this correct?

If I inspect the memory of t1 and t2, this is what I see:

t1: 03 00 00 00 c8 fd 12 00
t2: 04 00 00 00 c8 fd 12 00

So the "_i" member variable is behaving as expected, but the "_d" member variable appears to be behaving like a static member variable, i.e., class-scoped or shared among both objects.

I've been working a lot lately, so my brain may just be tired or incapable of thinking clearly right now. But this seems incorrect to me. Can someone help me!?
 
Your are assigning the reference to the temporary variable that is an argument for the constructor. The constructor should take a double&, not a double. So the behavior you witness might be because the compiler re-used the memory for the temporary value in the constructor when creating both t1 and t2.

So change the constructor to use double& and see what happens.
 
I discovered the same solution shortly after filing my post. Thanks.

I did a little research online and discovered that references do not require storage. That's what surprised me about the case as described. Even though I had a reference member variable, it was not given a unique storage location. Interesting
 
I may be misunderstanding you, but I think you are misunderstanding what happened. A reference takes up space, it is often implented as a pointer under the covers. Each reference itself was given unique storage location itself. What most likely happened was that both instances referred to the same memory location, which as far as you are concerned at a high level was full of garbage (it was just blind luck that it ended up being the same).

The real issue was that you were initializing a reference variable to a temporary object, and when that temporary object was destroyed, the value referred to by the reference was undefined.
 
Ah, yes. Couldn't see the forest for the trees. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top