
January 11th, 2006, 11:15 PM
| | | 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 ? | 
January 11th, 2006, 11:25 PM
| | | Re: Initialize a Reference type class attribute
On 11 Jan 2006 15:07:07 -0800, ken.carlino@gmail.com wrote:
[color=blue]
>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 ?[/color]
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 NoSpamPlease@Home.com | 
January 11th, 2006, 11:55 PM
| | | Re: Initialize a Reference type class attribute
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. | 
January 12th, 2006, 12:15 AM
| | | Re: Initialize a Reference type class attribute
<ken.carlino@gmail.com> skrev i meddelandet
news:1137023112.108074.208480@g44g2000cwa.googlegr oups.com...[color=blue]
> 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.[/color]
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.
[color=blue]
>
> 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?[/color]
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.
[color=blue]
>
> That is based on my understanding of item 21 "Use const whenever
> possible" in effecitive C++.[/color]
Use it as much as possible, but not more than that.
Bo Persson | 
January 12th, 2006, 12:25 AM
| | | Re: Initialize a Reference type class attribute
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&);
} | 
January 12th, 2006, 12:55 AM
| | | Re: Initialize a Reference type class attribute ken.carlino@gmail.com wrote:[color=blue]
> 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&);
>
> }[/color]
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 | 
January 12th, 2006, 12:55 AM
| | | Re: Initialize a Reference type class attribute
Thomas Tutone wrote:
[color=blue]
> 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.[/color]
Oops - I meant the copy constructor would not work on _const_ objects.
Best regards,
Tom | 
January 12th, 2006, 02:35 AM
| | | Re: Initialize a Reference type class attribute
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)
{
} | 
January 12th, 2006, 03:35 AM
| | | Re: Initialize a Reference type class attribute ken.carlino@gmail.com wrote:[color=blue]
>
> 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)
>
> {
>
> }[/color]
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 |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|