Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > flow between references , constructor and initialization list

Reply
Thread Tools

flow between references , constructor and initialization list

 
 
sks
Guest
Posts: n/a
 
      07-11-2006
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 ?

 
Reply With Quote
 
 
 
 
sonison.james@gmail.com
Guest
Posts: n/a
 
      07-11-2006
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

 
Reply With Quote
 
 
 
 
Robbie Hatley
Guest
Posts: n/a
 
      07-11-2006
"sks" <(E-Mail Removed)> 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/


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      07-11-2006
In article <(E-Mail Removed). com>,
"sks" <(E-Mail Removed)> 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.
 
Reply With Quote
 
loufoque
Guest
Posts: n/a
 
      07-11-2006
Robbie Hatley wrote :

> Sure you can have a reference to a reference.


No, you cant.

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

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      07-11-2006
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
 
Reply With Quote
 
loufoque
Guest
Posts: n/a
 
      07-11-2006
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.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      07-11-2006
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
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      07-12-2006
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.


 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      07-14-2006

"loufoque" <(E-Mail Removed)> 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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help on constructor/constructor initialization list with inheritanceusing templates balaji C++ 3 08-10-2011 12:06 PM
Query:difference between node flow and filter flow in java's I/O,system? Jack Dowson Java 0 05-07-2007 03:35 PM
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
understanding flow between object creation and member initialization list sks C++ 4 07-06-2006 10:28 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments