Assigning to references

Discussion in 'C++' started by Dave, Nov 7, 2003.

  1. Dave

    Dave Guest

    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
    want my reference member to refer to a new object; I'm not actually copying
    an object. Why should operator= come into play? After all, I can pass a
    reference to bar_t as a parameter just fine even though the copy constructor
    is also inaccessible.

    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?

    Thanks!
    Dave

    P.S. In case anyone is tempted to ask "What are you trying to do?", bar_t
    corresponds to ofstream and foo_t corresponds to one of my application
    classes. I need to contain an ofstream for logging, and I need to be able
    to change that stream occassionally (i.e. start logging to a different
    place).

    class bar_t
    {
    public:
    bar_t() {}

    private:
    bar_t(const bar_t &); // Leave undefined
    bar_t &operator=(const bar_t &); // Leave undefined
    };

    class foo_t
    {
    public:
    foo_t(): bar(initial_bar) {}
    void set_bar(bar_t &b) {bar = b;}

    private:
    bar_t initial_bar; // Must come *before* member bar as it is used to
    initialize bar.
    bar_t &bar;
    };



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Dave, Nov 7, 2003
    #1
    1. Advertising

  2. > 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
    > want my reference member to refer to a new object; I'm not actually

    copying
    > an object. Why should operator= come into play? After all, I can

    pass a
    > reference to bar_t as a parameter just fine even though the copy

    constructor
    > is also inaccessible.


    I think the error message put you on the wrong track. The compiler isn't
    complaining about the assignment operator of the bar_t class. The real
    problem is that C++ references in C++ are not assignable. They must
    always be initialized and cannot be reseated later.

    > 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?


    If you need the bar member to reference another object at some point in
    your program, the bar member must be a pointer.

    [comp.lang.c++.moderated cross-post removed since it slows down
    responses]

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Nov 7, 2003
    #2
    1. Advertising

  3. Dave

    Jack Adam Guest

    Dave wrote:
    > Hello all,


    > foo_t needs to contain a bar_t which is a class without a copy constructor
    > or operator=.
    >
    > 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
    > want my reference member to refer to a new object; I'm not actually copying
    > an object. Why should operator= come into play? After all, I can pass a
    > reference to bar_t as a parameter just fine even though the copy constructor
    > is also inaccessible.


    You cannot do that in C++; a reference always refers to the object with
    which it is initialized.

    > With that in mind, what's my next best alternative to create an
    > effect similar to what the code below attempts?


    Pointers can be reassigned to refer to different objects.

    HTH,
    Jack


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Jack Adam, Nov 7, 2003
    #3
  4. Dave

    Dan Cernat Guest

    "Dave" <> wrote in message
    news:...
    > 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?


    not much analisys done but one thought:
    references are *NOT* pointers. a reference is an alias (if you want) or a
    different name for a variable. you initialise it and use it like any other
    variable (use . not -> to call methods). The compiler may transform the
    reference into a pointer, but this is only an implementation detail. So,
    yes, you need an operator=

    my 2c

    /dan
    <snip>



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Dan Cernat, Nov 7, 2003
    #4
  5. Dave

    R.F. Pels Guest

    Dave wrote:

    > P.S. In case anyone is tempted to ask "What are you trying to do?", bar_t
    > corresponds to ofstream and foo_t corresponds to one of my application
    > classes. I need to contain an ofstream for logging, and I need to be able
    > to change that stream occassionally (i.e. start logging to a different
    > place).


    NOW you're telling us :)

    std::eek:fstream log("foo", somemode);
    .....
    log.close();
    log.open("bar", somemode);
    .....

    and in case you're afraid that someone uses your logging class in the mean
    time, use two references, one unused, create the new stream and assign to
    the unused reference, then assign the previously unused reference to the
    previously used reference.
    --

    Ruurd


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    R.F. Pels, Nov 7, 2003
    #5
  6. Dave

    Cy Edmunds Guest

    "Dave" <> wrote in message
    news:...
    > 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? [snip]


    Because you can't reassign a reference in C++. I have already given you the
    answer to this problem twice now in a previous thread.

    --
    Cycho{HHR}
    http://home.rochester.rr.com/cyhome/


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Cy Edmunds, Nov 7, 2003
    #6
  7. Hello Dave,

    Dave schrieb:

    > 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
    > want my reference member to refer to a new object; I'm not actually copying
    > an object. Why should operator= come into play? After all, I can pass a
    > reference to bar_t as a parameter just fine even though the copy constructor
    > is also inaccessible.


    A reference in C++ is very near what we would name an "alias" of what it is
    refering to. After initialization bar **is** the object of type T it was
    initialized with,
    thus an assignment to it calls the copy assignment operator of T.
    References as function parameters behave as a transfer capsule from its actual
    argument to the function internal accessor. The call

    bar_t bobject;
    ....
    set_bar(bobject);

    creates a reference which binds to bobject. This reference is now available
    inside
    set_bar and no copy operation takes during function entry. This case is
    different from

    bar_t bobject;
    ....
    bar_t& bref = bobject;

    bref = bobject;

    where the (non-initialization) assignment effectivly copies bobject into itself.

    References as function parameters were quite useless, if they would call the
    copy assignment operator of T, because this would make them not different
    from "call-by-value".

    > 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?


    Use a pointer instead of a reference. Pointer do have two states (assigned and
    not assigned) and thus can change between states (Reassignment is also allowed).

    In many cases a reference is more appropriate than a pointer, because you don't
    need to test, whether it is assigned or not: Once initialized it always is
    assigned to the
    same object!

    In your case you want to allow a redirection of the target, you are "refering"
    to,
    so you need a pointer. This also makes you responsible for checking whether
    the given pointer is a null pointer constant (not assigned) or not (assigned).

    Hope that helps,

    Daniel Spangenberg




    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Daniel Spangenberg, Nov 7, 2003
    #7
  8. "Dave" <> wrote in message news:<>...
    > Hello all,
    >
    > 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


    In C++, once a reference is seated, it can not be re-seated. You may
    modify the referant, but you may not make it refer to a different
    object.

    "bar = b" calls operator= on 'bar', passing in 'b'.

    It has to be this way. If 'bar=b' re-seated 'bar', how would you call
    operator=?

    If you want to have a reference and change what it refers to, use a
    pointer:

    pbar=&b;

    *pbar << "hello" << endl;

    joshua lehrer
    factset research systems
    NYSE:FDS

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Joshua Lehrer, Nov 7, 2003
    #8
  9. Hi,

    Dave wrote:

    > 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.


    > class bar_t
    > {


    > private:


    > bar_t &operator=(const bar_t &); // Leave undefined


    That's why.
    You cannot assgn to bar_t, because you have explicitly forbid it.
    Note that:

    - you cannot "reseat" the reference, and
    - the reference always denotes some object

    so the instruction:

    bar = b;

    tries to *assign* what is denoted by 'b' to what is denoted by 'bar'. In
    other words, the above instruction tries to assign one bar_t object to
    another, which is explicitly forbidden.

    If you need to achieve the effect of reseating (without the possibility
    to assign), consider using pointers instead of references.

    I've used pointers recently in exactly the same context (logging ostream
    object associated with another object).

    > class foo_t
    > {
    > public:
    > foo_t(): bar(initial_bar) {}
    > void set_bar(bar_t &b) {bar = b;}


    bar = &b;

    > private:
    > bar_t &bar;


    bar_t *bar;

    > };


    --
    Maciej Sobczak : http://www.msobczak.com/
    Programming : http://www.msobczak.com/prog/


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Maciej Sobczak, Nov 8, 2003
    #9
  10. Dave wrote:

    > 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 want my reference member to refer to a new object;
    > I'm not actually copying an object. Why should operator= come into
    > play? After all, I can pass a reference to bar_t as a parameter just
    > fine even though the copy constructor is also inaccessible.


    The problem is that references cannot be reassigned. Luckily enough, the
    solution is simple: use a pointer.

    Gerhard Menzl


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Gerhard Menzl, Nov 8, 2003
    #10
  11. On Thu, 06 Nov 2003 20:26:30 -0500, Dave wrote:

    > 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;}


    class foo_t {
    public:
    foo_t(bar_t *b);
    void set_bar(bar_t *b) {bar = b;}
    void set_bar(bar_t &b) {bar = &b;}
    private:
    bar_t *bar;
    }



    >
    > 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
    > want my reference member to refer to a new object; I'm not actually copying
    > an object. Why should operator= come into play? After all, I can pass a
    > reference to bar_t as a parameter just fine even though the copy constructor
    > is also inaccessible.


    You are calling the assignment operator, when you do "bar = b". The copy
    constructor has nothing to do with it.

    > P.S. In case anyone is tempted to ask "What are you trying to do?", bar_t
    > corresponds to ofstream and foo_t corresponds to one of my application
    > classes. I need to contain an ofstream for logging, and I need to be able
    > to change that stream occassionally (i.e. start logging to a different
    > place).



    class Logger {
    public:
    Logger();

    void setStream(ostream &stream);

    ostream& stream();
    ostream& operator << (...);
    private:
    ostream *myStream;
    }

    ....
    Logger log(std::cout);

    log.stream() << "blah\n";



    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Nils Petter Vaskinn, Nov 8, 2003
    #11
  12. Dave

    JarlOstensen Guest

    "Dave" <> wrote in message news:<>...
    > Hello all,
    >

    Hi Dave,

    The answer is short and harsh I'm afraid. The Standard, (8.5.3-2),
    states:

    "A reference can not be changed to refer to another object
    after initialization."

    and this is exactly what you have hit upon. The compiler is happy
    initialising
    your reference in the ctor of foo_t, but after that it will not allow
    bar to change. So, when you say

    bar = b

    the compiler resolves this to

    deref(bar)->operator=( b );

    which fails since operator= is private.

    -=jarl

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    JarlOstensen, Nov 8, 2003
    #12
  13. Dave

    David Turner Guest

    "Dave" <> wrote in message
    news:...
    > 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! ]
     
    David Turner, Nov 8, 2003
    #13
  14. Dave wrote:
    <snip>
    > 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?

    <snip>

    You misunderstand what a reference is. Assigning to a reference
    means assigning to the object it refers to, not binding the
    reference to another object.

    You need to store a pointer instead, and use unary * and &
    operators to convert between pointers and references.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ben Hutchings, Nov 8, 2003
    #14
  15. In article <>, Dave
    <> writes
    >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
    >want my reference member to refer to a new object; I'm not actually copying
    >an object. Why should operator= come into play? After all, I can pass a
    >reference to bar_t as a parameter just fine even though the copy constructor
    >is also inaccessible.


    You have misunderstood what a reference is in C++. It is purely a name
    that is bound to an existing object and the language does not provide a
    normal mechanism for rebinding the name to a different object.
    operator=() deals with objects and values not with names so bar = b
    requires that the value of b be assigned to the object referred to by
    bar. As there is no operator=() for the type in question this cannot be
    done.

    The special property of reference parameters is that binding between
    name and object takes place as part of the process of calling the
    function so that parameter name can be bound to many different objects,
    but each time that binding is for the duration of the function call.

    The best you can do is to explicitly change any publicly writable
    characteristics of the object:

    void change_bar(bar_t& b){
    bar.write_val1(b.read_val1());
    bar.write_val2(b.read_val2());
    etc.
    }

    But be very careful if your process is multi-threaded because it seems
    that bar is a global variable and so the above process must be protected
    by something such as a mutex.


    --
    Francis Glassborow ACCU
    If you are not using up-to-date virus protection you should not be reading
    this. Viruses do not just hurt the infected but the whole community.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Francis Glassborow, Nov 8, 2003
    #15
  16. "Dave" <> wrote in message news:<>...
    > 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
    > want my reference member to refer to a new object; I'm not actually copying
    > an object. Why should operator= come into play? After all, I can pass a
    > reference to bar_t as a parameter just fine even though the copy constructor
    > is also inaccessible.


    You cannot reset a reference. The instruction above tries to assign
    directly to bar. The solution is to define bar as a pointer to a bar_t
    and change set_bar() to

    void set_bar(bar_t &b) {bar = &b;}

    Cheers,
    Nicola Musatti

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Nicola Musatti, Nov 8, 2003
    #16
  17. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Dave wrote:

    | 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
    | want my reference member to refer to a new object; I'm not actually
    copying
    | an object. Why should operator= come into play? After all, I can pass a
    | reference to bar_t as a parameter just fine even though the copy
    constructor
    | is also inaccessible.

    You never assign *to* references, you assign *through* references.

    A reference is a different name for an object, and must be bound
    when created, and can never be rebound.

    References are second class citizens in the C++ world.

    You cannot have references to references, you cannot have pointers
    to references, if you take the address of a reference (operator&)
    you get the address of the referent that the reference is bound
    to, etc...

    If you're familiar with Pascal, a reference is like adding the
    keyword "var" in front of a parameter in a parameter list.

    procedure foo(a: integer); begin ... end;
    procedure bar(var a: integer); begin ... end;

    In C++, we would say:

    void foo(int a) { ... }
    void bar(int &a) { ... }

    In either case, the name "a" in procedure "bar" is, for
    all practical purposes, an int-typed variable; it just happens
    to be stored differently from the "a" in "foo".

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQE/q7WOJJVk0d/xqeYRAonUAJ9alYAEtsPy03NSZu+ne59JT2efBACgiPUN
    VhJt7JCc1kTXFEKhU6CNTks=
    =ZDoM
    -----END PGP SIGNATURE-----

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Antoun Kanawati, Nov 8, 2003
    #17
  18. Dave

    Simon Bone Guest

    On Thu, 06 Nov 2003 20:26:30 -0500, Dave wrote:

    > 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;}
    >


    C++ references are just aliases to existing objects; they cannot be
    reseated. Try using a pointer instead, since that has the meaning you are
    after here.

    HTH,

    Simon Bone


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Simon Bone, Nov 8, 2003
    #18
  19. Dave

    Ron Guest

    > 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
    > want my reference member to refer to a new object; I'm not actually copying
    > an object. Why should operator= come into play? After all, I can pass a
    > reference to bar_t as a parameter just fine even though the copy constructor
    > is also inaccessible.
    >
    > class bar_t
    > {
    > public:
    > bar_t() {}
    >
    > private:
    > bar_t(const bar_t &); // Leave undefined
    > bar_t &operator=(const bar_t &); // Leave undefined
    > };
    >
    > class foo_t
    > {
    > public:
    > foo_t(): bar(initial_bar) {}
    > void set_bar(bar_t &b) {bar = b;}
    >
    > private:
    > bar_t initial_bar; // Must come *before* member bar as it is used to
    > initialize bar.
    > bar_t &bar;
    > };


    The compiler accepts the line

    foo_t(): bar(initial_bar) {}

    -- even though bar_t has no accessible copy constructor -- because
    this line binds the reference "bar" to the member variable
    "initial_bar". The line

    void set_bar(bar_t &b) {bar = b;}

    fails to compile because it's asking the compiler to copy the object
    "b" into the object to which the reference "bar" is bound.

    Huh? That's right: references can be bound only once. After they're
    bound, they refer to the same object forever*. Section 8.5.3(2):

    A reference cannot be changed to refer to another object after
    initialization. Note that initialization of a reference
    is treated very differently from assignment to it....

    Thus:

    int i;
    int &r = i; // r is now an alias for i
    r = 3; // i now == 3. r DOES NOT refer to the rvalue "3".
    r = 6; // i now == 6

    -Ron

    * Putting aside nasssty treacherous rebinding tricks using explicit
    destructor calls and placement new.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ron, Nov 8, 2003
    #19
  20. Dave

    Dave Guest

    "Dave" <> wrote in message
    news:...
    > 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
    > want my reference member to refer to a new object; I'm not actually

    copying
    > an object. Why should operator= come into play? After all, I can pass a
    > reference to bar_t as a parameter just fine even though the copy

    constructor
    > is also inaccessible.
    >
    > 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?
    >
    > Thanks!
    > Dave
    >
    > P.S. In case anyone is tempted to ask "What are you trying to do?", bar_t
    > corresponds to ofstream and foo_t corresponds to one of my application
    > classes. I need to contain an ofstream for logging, and I need to be able
    > to change that stream occassionally (i.e. start logging to a different
    > place).
    >
    > class bar_t
    > {
    > public:
    > bar_t() {}
    >
    > private:
    > bar_t(const bar_t &); // Leave undefined
    > bar_t &operator=(const bar_t &); // Leave undefined
    > };
    >
    > class foo_t
    > {
    > public:
    > foo_t(): bar(initial_bar) {}
    > void set_bar(bar_t &b) {bar = b;}
    >
    > private:
    > bar_t initial_bar; // Must come *before* member bar as it is used to
    > initialize bar.
    > bar_t &bar;
    > };


    Hello all,

    I had sent an earlier post that for some reason didn't get through (and some
    of you may have noticed that for some reason, my original post got posted
    twice; sorry about that...). So, I'm having some trouble here. In any
    case, I just wanted to thank everyone for all of the responses. I can't
    believe I had such a massive mental lapse! Your points are very well made
    and very well taken!

    As long as I'm writing, I'll make one more query. This is purely out of
    curiosity - I'm not trying to do anything specific. And of course, if there
    *is* a way to do what I'm about to ask about, it would no doubt be very
    ill-advised so, again, this is purely out of curiosity...

    One poster mentioned the possibility of re-binding a reference by destroying
    the old rerferent with a manual destructor call and then using placement new
    to construct the new object in the same memory. Though ill-advised, this
    does sound like a standard-conforming way to do it. This got me to
    wondering...

    Is there some sort of treacherous trick that would allow us to get the
    address of the reference (as opposed to the referent)? Is a reference even
    guaranteed to *have* an address, or might it be just a simple compile-time
    alias for the referent? I'd be interested to here any thoughts on all of
    this...

    Thanks again,
    Dave
     
    Dave, Nov 8, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    446
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    458
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    616
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    720
    Bruno Desthuilliers
    Dec 12, 2006
  5. weston
    Replies:
    1
    Views:
    253
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page