473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Initialize a Reference type class attribute

Hi,

Can I use Reference type as my class attribute, like this? Or I have to
use pointers for class attribute?
class B;
class A {

public:
B& _b;
};
If yes, how can I init the class attribute?

I try this:

A::A(const B& b)
{
_b = b;
}

I get this error "error: uninitialized reference member "

I try this:
A::A (const B& b) :
_b(b)

{

}
I get this error "error: invalid initialization of reference of type "

or I have to use Pointers ?

Jan 11 '06 #1
8 10805
On 11 Jan 2006 15:07:07 -0800, ke*********@gmail.com wrote:
Hi,

Can I use Reference type as my class attribute, like this? Or I have to
use pointers for class attribute?
class B;
class A {

public:
B& _b;
};
If yes, how can I init the class attribute?

I try this:

A::A(const B& b)
{
_b = b;
}

I get this error "error: uninitialized reference member "

I try this:
A::A (const B& b) :
_b(b)

{

}
I get this error "error: invalid initialization of reference of type "

or I have to use Pointers ?


Your B& member is non-const, therefore you cannot initialize it with a
const B&. The second try would have worked if you had declared your
constructor to take a B& and not a const B&.

--
Bob Hairgrove
No**********@Home.com
Jan 11 '06 #2
Thanks.
I thought by saying "A::A(const B& b) {...}", it means i can't change
what 'b' is pointing to, not b is pointing to a constant B.

this is kind of like when we overload operator, we do this:
const A operator* (const A& lhs, const A& rhs);

or it is totally different?

That is based on my understanding of item 21 "Use const whenever
possible" in effecitive C++.

thank you.

Jan 11 '06 #3

<ke*********@gmail.com> skrev i meddelandet
news:11**********************@g44g2000cwa.googlegr oups.com...
Thanks.
I thought by saying "A::A(const B& b) {...}", it means i can't
change
what 'b' is pointing to, not b is pointing to a constant B.
But b isn't pointing, it is a reference to a B. :-)

A reference is like another name for the original object.

What look like an assignment to _b,

_b = something();

is really an assignment to the object _b refers to. And if that object
is const, assigning it through _b would be totally wrong.

this is kind of like when we overload operator, we do this:
const A operator* (const A& lhs, const A& rhs);

or it is totally different?
It's similar, but different. :-)

Consider how your A class is supposed to be used:

const B some_B;

A a = some_B;

Now a's member _b is an alias for some_B, which is const. Assigning

a._b = something();

would actually try to assign the value to some_B. Not allowed.


That is based on my understanding of item 21 "Use const whenever
possible" in effecitive C++.


Use it as much as possible, but not more than that.
Bo Persson

Jan 12 '06 #4
A related question, why when we declare a copy constructor, we always
put "const in front of the reference"?
like this:

class Account {
public:
Account (const Account&);

}

Jan 12 '06 #5
ke*********@gmail.com wrote:
A related question, why when we declare a copy constructor, we always
put "const in front of the reference"?
like this:

class Account {
public:
Account (const Account&);

}


It's not required, but it's generally good practice if (as is usually
the case) the copy constructor does not modify the original object.
Take a look at the FAQ on this topic:

http://www.parashift.com/c++-faq-lit...rrectness.html

Among other things, if you didn't include the const, the copy
constructor would not work on non-const objects, which could be a
significant issue.

std::auto_ptr<> is an example of a template class from the standard
library that uses a non-const copy constructor. And its behavior often
surprises people not previously familiar with it.

Best regards,

Tom

Jan 12 '06 #6

Thomas Tutone wrote:
Among other things, if you didn't include the const, the copy
constructor would not work on non-const objects, which could be a
significant issue.


Oops - I meant the copy constructor would not work on _const_ objects.

Best regards,

Tom

Jan 12 '06 #7
Thanks. That is what I understand too. "it's generally good practice if
(as is usually
the case) the copy constructor does not modify the original object. "

Back to my original quesiton, why I can't do this. It only works if I
remove the 'const' in the input parameter of the Constructor. I do not
modify the input parameter of the constructor, why I can't add 'const'
in the input parameter.

class B;
class A {

public:
B& _b;
};
A::A (const B& b) :
_b(b)

{

}

Jan 12 '06 #8
ke*********@gmail.com wrote:

Back to my original quesiton, why I can't do this. It only works if I
remove the 'const' in the input parameter of the Constructor. I do not
modify the input parameter of the constructor, why I can't add 'const'
in the input parameter.

class B;
class A {

public:
B& _b;
};
A::A (const B& b) :
_b(b)

{

}


Because _b is a reference to (i.e., an alias for) the passed argument.
That means if anyone makes any changes to _b, that is really a change
in the passed argument. So basically, you can make your class in two
different ways:

class A {
B& b_; // no const
public:
A(B& b) : b_(b) {} // no const
};

or

class A {
const B& b_; // note const
public:
A(const B& b) : b_(b) {} // note const
};

In other words, since it's a reference, const must appear in both
places or neither.

Best regards,

Tom

Jan 12 '06 #9

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

Similar topics

3
by: Pablo Gomes Ludermir | last post by:
Hello, I have the following case that I am trying to put in XML Schema. I have the classes HelpItem, Document and Message that work as follows: One HelpItem contains several Document and...
3
by: Tony Johansson | last post by:
Hello!! Hello Experts! I'm right if I say that if I have attribute that is constant or attribute that is of reference type or attribute that is class type then I must use initialize list. ...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
3
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
8
by: wASP | last post by:
Hi, I'm having a problem referencing the elements within an object after a method of that object (a member function) has been activated with an onsubmit handler: - - - - - - - - ...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
4
by: Bram Kuijper | last post by:
Okay, second try (since my posting on 4/27), to find a proper way to initialize a static reference member to an object. I try to initialize a static reference inside the class ga, referencing to...
8
by: chamalulu | last post by:
Hello. I think I'm aware of how attribute access is resolved in python. When referencing a class instance attribute which is not defined in the scope of the instance, Python looks for a class...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.