Re: Assigning to references

From: David Turner (david_at_firepro.co.za)
Date: 11/08/03


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! ]



Relevant Pages

  • Re: DLL-Function with Variant used in C#
    ... is passing a reference to the variant. ... Additionally, the return value is a pointer, not an int, so you have to ... private static extern int GetGlobalValue ... I have a Problem with C Sharp. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: REFERENCES REVEALED
    ... A pointer is a variable which holds a memory address. ... A reference is another name to an existing object ... int main ...
    (comp.lang.cpp)
  • Re: Copy Constructor and other questions
    ... Consider passing a pointer to a class derived from A??? ... Well, you can return a reference to NULL, but then the burden is on the ... > int main ...
    (comp.lang.cpp)
  • reference VS. pointer
    ... int i = 12; ... In order to access the value of i through pointer p, ... use the value of p as the address of a memory chunk; ... If reference r is used to access the value of i, ...
    (comp.lang.cpp)
  • class __getitem__ when item is not a sequence ???
    ... How can I use an instance's data by reference to the instance name, ... instance of an integer class. ... Since it refers to only a single value ... type like int, float etc. ...
    (comp.lang.python)

Loading