From: Michael Aaron Safyan on
smawsk wrote:
> Hi,
>
> I am writing a sample program to use a reference member variable. But
> I was surprised to know that the below piece of code is not working as
> I expected.
> Can some one let me know what's wrong with this code?
>
> class X
> {
> public:
> X(int x) : ref(x)
> {
>
> }
> void Display() const
> {
>
> cout<<ref<<endl;
> }
>
> private:
> int& ref;
> };
>
> int main()
> {
> int c = 10;
> X x(c);
> x.Display(); // This function does not print 10. Instead some junk
> value
>
> return 0;
> }
>
> Why So? Am I missing some basic point regarding the working of
> References here?
>
> Thanks in advance....
>
> Warm Regards.
>

A reference must be initialized from a reference or pointer object.
X::X(int) does not take a reference or pointer. What you are doing is
initializing X::ref with the address of "x" in X::X(int), which is
located in the stack frame of the function X::X(int) and which becomes
invalid when X::X(int) has completed execution. Consider the following:


int c = 10;

---
c |10 |
---

X x(c);

---
c |10 |
---
x |10 | <= Not the same address as c!
---
ref|&x | <= Not pointing to c!
---

// After execution of X x(c);

---
c |10 |
---
ref|? | <= The object "x" no longer exists! This references is invalid.
---

// ... You get the point.


What I think you intend to do is:

class X
{
public:
X(int& x) : _ref(&x) {}
~X(){}
int get()const{ return _ref: }
void print(){ std::cout<<get()<<std::endl; }
private:
X& operator=(const X& o){ return *this; }
int& _ref;
};


int main(int argc, char* argv[])
{
int c = 10;
X x(c);
x.print();
return 0;
}


{ Please convert tab characters to spaces before posting. -mod }

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Tom on
On 28 Mrz., 18:09, smawsk <sk.sma...(a)gmail.com> wrote:
> Why So? Am I missing some basic point regarding the working of
> References here?

Nice one. Didn't get it until I debugged it. Note the constructor:
X(int x) : ref(x)

You're intialize "ref" with "x". But here, x is already a local
varible. It's not the one from the main function. And the local
variable is getting destroyed soon and your reference is bogus.

What you want is this:
X(int &x) : ref(x)

That should work.

Tom
----------------------------------
Eliminar duplicados en Outlook:
http://www.easy2sync.com/es/produkte/1-Click-Duplicate-Delete-Outlook.php

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mateusz Adamczyk on
On 28 Mar, 18:09, smawsk <sk.sma...(a)gmail.com> wrote:
> X(int x) : ref(x)
> {
In the code above, You create reference to temporary variable x,
which will be probably destroyed before destruction of a x object.
If Your object should hold reference to a c variable,
You should write:
X(int& x) : ref(x){}
and everything should be all right.

Regards,
Mateusz Adamczyk

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Matthew Gilbert on
On Mar 28, 1:09 pm, smawsk <sk.sma...(a)gmail.com> wrote:
> X(int x) : ref(x)

x is passed by value to X's constructor. Therefore, ref(x) refers to
an int that disappears at the end of the constructor.

You could fix it by having X's constructor take an int&. That way, ref
refers to c. Of course, then x is only valid for as long as c is
valid.

_matt


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: david on
On 28 mar, 18:09, smawsk <sk.sma...(a)gmail.com> wrote:
> Hi,
>
> I am writing a sample program to use a reference member variable. But
> I was surprised to know that the below piece of code is not working as
> I expected.
> Can some one let me know what's wrong with this code?
>
> class X
> {
> public:
> X(int x) : ref(x)
> {
>
> }
> void Display() const
> {
>
> cout<<ref<<endl;
> }
>
> private:
> int& ref;
>
> };
>
> int main()
> {
> int c = 10;
> X x(c);
> x.Display(); // This function does not print 10. Instead some junk
> value
>
> return 0;
>
> }
>
> Why So? Am I missing some basic point regarding the working of
> References here?

{ edits: quoted signature & clc++m banner removed please don't quote extraneous
material -mod }

Hello,

X's constructor takes an int by value, so the member variable ref
references a local (to the constructor) copy of the passed int.
Further use of the reference outside the body of X's constructor is
undefined behavior.

Regards,
David

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]