Re: Assigning to references
From: David Turner (david_at_firepro.co.za)
Date: 11/08/03
- Next message: Ben Hutchings: "Re: Assigning to references"
- Previous message: JarlOstensen: "Re: Assigning to references"
- In reply to: Dave: "Assigning to references"
- Next in thread: Ben Hutchings: "Re: Assigning to references"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Nov 2003 19:43:17 -0500
"Dave" <better_cs_now@yahoo.com> wrote in message
news:vqkohu1r9sk416@news.supernews.com...
> 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 within my control to change bar_t. Furthermore,
I
> need to be able to update the contained bar_t at runtime (hence the
> set_bar() method seen below).
>
> The code *almost* works. Here's the problematic line:
>
> void set_bar(bar_t &b) {bar = b;}
>
> This fails to compile with a message that operator= is inaccessible. Why
> should this be a problem since I'm trying to assign to a reference? I
only
You Can't Change References. Once it has been constructed, a reference
behaves exactly like the object it refers to (it is an "alias" for that
object). Assigning to it is exactly the same as assigning to the original
object. Consider this code:
int a = 1;
int& b = a; // b refers to a
b = 2; // Now a == 2. "b" is simply an alias for "a".
int& c; // This is an error. You can't have a reference that doesn't
refer to anything.
int& d = b; // d refers to b, which is the same as a
b = d; // Exactly equivalent to "a = a".
Contrast this with the behaviour of pointers:
int a = 1;
int* b = &a; // b points to a
*b = 2; // Now a == 2.
int c = 3;
int* d = &c; // d points to c
b = d; // b points to whatever d points to (i.e. c)
c = 4;
assert(*b == 4);
// Note the contrast of *b, meaning the object b points to, and b, the
pointer itself.
// References make no such distinction; you are always referring to the
referenced object.
> Assuming though that my compiler is behaving properly, I won't be able to
> take this approach regardless of whether or not I understand why it's
> disallowed. With that in mind, what's my next best alternative to create
an
> effect similar to what the code below attempts?
What you probably meant to do is have the member bar be a *pointer*.
For example:
class foo_t
{
bar_t* bar;
public:
foo_t(): bar(0) { }
void set_bar(bar_t& b) { bar = &b; }
void do_something() { bar->do_something(); }
};
Bear in mind that, unlike a reference, a pointer will not extend the
lifetime of the object it points to. So if you were to write code like
this:
void give_me_a_bar(foo_t& foo)
{
bar_t bar;
foo.set_bar(bar);
}
int main()
{
foo_t foo;
give_me_a_bar(foo);
foo.do_something(); // crash here
}
then the Result Is Undefined.
Regards
David Turner
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
- Next message: Ben Hutchings: "Re: Assigning to references"
- Previous message: JarlOstensen: "Re: Assigning to references"
- In reply to: Dave: "Assigning to references"
- Next in thread: Ben Hutchings: "Re: Assigning to references"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|