Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   flow between references , constructor and initialization list (http://www.velocityreviews.com/forums/t455325-flow-between-references-constructor-and-initialization-list.html)

sks 07-11-2006 12:43 PM

flow between references , constructor and initialization list
 
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 ?


sonison.james@gmail.com 07-11-2006 01:06 PM

Re: flow between references , constructor and initialization list
 
sks wrote:
> 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


Robbie Hatley 07-11-2006 01:17 PM

Re: flow between references , constructor and initialization list
 
"sks" <eshambhu@gmail.com> wrote:

> 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/



Daniel T. 07-11-2006 01:24 PM

Re: flow between references , constructor and initialization list
 
In article <1152621790.343106.37900@h48g2000cwc.googlegroups. com>,
"sks" <eshambhu@gmail.com> wrote:

> 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.

loufoque 07-11-2006 01:57 PM

Re: flow between references , constructor and initialization list
 
Robbie Hatley wrote :

> Sure you can have a reference to a reference.


No, you cant.

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


Frederick Gotham 07-11-2006 03:08 PM

Re: flow between references , constructor and initialization list
 
loufoque posted:

> Robbie Hatley wrote :
>
>> Sure you can have a reference to a reference.

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




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



--

Frederick Gotham

loufoque 07-11-2006 04:25 PM

Re: flow between references , constructor and initialization list
 
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.

Frederick Gotham 07-11-2006 04:38 PM

Re: flow between references , constructor and initialization list
 
loufoque posted:

> 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.



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.

--

Frederick Gotham

Ron Natalie 07-12-2006 11:55 AM

Re: flow between references , constructor and initialization list
 
Robbie Hatley wrote:

>
> 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.



Robbie Hatley 07-14-2006 11:14 AM

Re: flow between references , constructor and initialization list
 

"loufoque" <loufoque@remove.gmail.com> wrote:

> Robbie Hatley wrote :
>
> > Sure you can have a reference to a reference.

>
> 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/




All times are GMT. The time now is 10:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.