473,473 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

flow between references , constructor and initialization list

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 ?

Jul 11 '06 #1
9 1727
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

Jul 11 '06 #2
"sks" <es******@gmail.comwrote:
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/
Jul 11 '06 #3
In article <11*********************@h48g2000cwc.googlegroups. com>,
"sks" <es******@gmail.comwrote:
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.
Jul 11 '06 #4
Robbie Hatley wrote :
Sure you can have a reference to a reference.
No, you cant.

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

Jul 11 '06 #5
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
Jul 11 '06 #6
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.
Jul 11 '06 #7
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
Jul 11 '06 #8
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.
Jul 12 '06 #9

"loufoque" <lo******@remove.gmail.comwrote:
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/
Jul 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Dave | last post by:
Hello all, Please consider the code below. It is representative of a problem I am having. foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not...
6
by: cppaddict | last post by:
Hi, I know that C++ does not have an explicit super() constructor for calling a Base class constructor from a Derived class's constructor, but my understanding is that C++ implements this...
18
by: Makis Papapanagiotou | last post by:
Hello all, There is a strange case where a class Test is composed from three objects of other classes. i.e. class Test { public: Test(); private: Point x;
6
by: Dhananjaya.R | last post by:
Hi, What will be the constructor flow of control for the below snippet. I had an hard time understanding how C# can allow initialization of member variables in class declaration. public class...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
6
by: rona | last post by:
Hi everyone, Could someone tell us why the below code compiles and works? Is it legal in the constructor of class B? Cheers Ronny ---------------------------------
4
by: sks | last post by:
hi , i Have a code snippet as follows class ABC { int &r; ABC(int a=0): r(a) {} }; int main() {
4
by: Matteo Migliore | last post by:
Hi. I've a problem with parent-child circular references. Suppose have two classes, Person and Course: -------------------------- public class Person{ public Person(string name, Course...
6
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.