flow between references , constructor and initialization list

S

sks

dear all,
i have a problem . consider the following code
class ABC
{
int & r;
public: ABC( int &a) : r(a) {}
};

int main()
{
int d=10;
ABC Myobj(d)
}


i cannot understand how this works and Have two question in my mind
1) we cannot have reference to reference . but here we have a as
refrerence to d and r is reference to a . how is this possible ?

2) It is generally known that object is created and then constructor is
called . and references have to be initialised immediately . so my
question since My Obj is created then Constructor is called that means
the object is created in the memory and still the reference is not
initialised . How is this possible ?
 
S

sonison.james

sks said:
dear all,
i have a problem . consider the following code
class ABC
{
int & r;
public: ABC( int &a) : r(a) {}
};

int main()
{
int d=10;
ABC Myobj(d)
}


i cannot understand how this works and Have two question in my mind
1) we cannot have reference to reference . but here we have a as
refrerence to d and r is reference to a . how is this possible ?
a is more correctly a reference to an int so is r. r is not a reference
to a, the constructor initializer list is used to initialize r with a's
value.
2) It is generally known that object is created and then constructor is
called . and references have to be initialised immediately . so my
question since My Obj is created then Constructor is called that means
the object is created in the memory and still the reference is not
initialised . How is this possible ?
The constructor intializer list is invoked before the constructor
(body) is invoked. So at no point withing the constructor is a
reference uninitialized. In fact all references must be initialized by
the constructor initializer list if they are to be used by the class.


Thanks and regards
SJ
 
R

Robbie Hatley

sks said:
i have a problem . consider the following code
class ABC
{
int & r;
public: ABC( int &a) : r(a) {}
};

int main()
{
int d=10;
ABC Myobj(d)
}


i cannot understand how this works and Have two question in my mind
1) we cannot have reference to reference . but here we have a as
refrerence to d and r is reference to a . how is this possible ?

Sure you can have a reference to a reference. What do you think
the following is:

std::cout << a << b << c << d << std::endl;

It's a reference to a reference to a reference to a reference to a
reference to an iostream.

You can chain references as many deep as you want. What really
happens is, all levels in the chain really refer back to the
original item:

int a = 7;
int & b = a; // b refers to a. b -> a
int & c = b; // c refers to a. c -> b -> a
int & d = c; // d refers to a. d -> c -> b -> a
d = 32; // changes a to 32
std::cout << a << std::endl; // prints "32"
2) It is generally known that object is created and then constructor is
called . and references have to be initialised immediately . so my
question since My Obj is created then Constructor is called that means
the object is created in the memory and still the reference is not
initialised . How is this possible ?

I'd say the object is not fully "created" until the constructor
is finished running.

In any case, your code makes me nervous. I don't like objects
that have references in them to a local variable in some function.
If that variable goes out of scope, the reference is now invalid.
In your case, you get away with it only because the variable is in
main().

A reference in an object to a static variable in a function is
safer.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
D

Daniel T.

"sks said:
dear all,
i have a problem . consider the following code
class ABC
{
int & r;
public: ABC( int &a) : r(a) {}
};

int main()
{
int d=10;
ABC Myobj(d)
}


i cannot understand how this works and Have two question in my mind
1) we cannot have reference to reference . but here we have a as
refrerence to d and r is reference to a . how is this possible ?

2) It is generally known that object is created and then constructor is
called . and references have to be initialised immediately . so my
question since My Obj is created then Constructor is called that means
the object is created in the memory and still the reference is not
initialised . How is this possible ?

Your general knowledge is incorrect for C++. In C++ the object isn't
"created" until after the constructor exists normally. If the
constructor throws an exception, for example, the object isn't created.
 
F

Frederick Gotham

loufoque posted:
Robbie Hatley wrote :


No, you cant.

int& & is an invalid type (for now).



You've quoted Robbie out of context. His post made perfect sense.
 
L

loufoque

Frederick Gotham wrote :
You've quoted Robbie out of context. His post made perfect sense.

It would be more exact to say he didn't understand what a reference to a
reference was.
 
F

Frederick Gotham

loufoque posted:
Frederick Gotham wrote :


It would be more exact to say he didn't understand what a reference to a
reference was.


This is word play.

If you were to ask me, "Can you have a reference to a reference?", then I'd
tell you that the question is dubious. In the following sense, you *can* have
a reference to a reference:

int i;

int &r = i;

int &s = r;

/* "s" is a reference, and "r" is a reference.
The thing on the left refers to the thing on
the right. Therefore: a reference to a reference.
*/


However, we all know that there's really no such thing as a reference... it's
just a puff of air that mystically achieves our objective. If we re-examine
the line:

int &s = r;

, then we see that "r" is in fact a reference... BUT, once you've bound a
reference to an object, any dealings with the reference are actually dealings
with the referred object.

The intuitive and intellectual answer is that "No, you can't have a reference
to a reference.", but you can't reprehend someone straight away for stating
otherwise.
 
R

Ron Natalie

Robbie said:
It's a reference to a reference to a reference to a reference to a
reference to an iostream.
Nope. You can initialize a reference from another reference, but what
you end up with is a reference to the referred to object.
 
R

Robbie Hatley

loufoque said:
Robbie Hatley wrote :


No, you cant.
int& & is an invalid type (for now).

OK, I agree that it perhaps is best to not use the phrase
"reference to a reference" when meaning a chain of references,
all of which refer back to the original referent, because
someday maybe C++ WILL allow the "int&&" type of "reference
to a reference":

int a = 7;
int& b = a;
int& c = b; // c refers to a, not to b
int&& d = c; // d refers to c, not to a

Perhaps I should call c "reference initialized from a reference",
whereas the hypothetical d would be "reference to a reference".

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top