Assigning to a reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    Assigning to a reference

    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=(cons t 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;
    };


  • Julián Albo

    #2
    Re: Assigning to a reference

    Dave escribió:
    [color=blue]
    > 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[/color]

    References are not assignables, when you assign to one you are really
    assigning to the object refered by it.

    Use a pointer instead of a reference.

    Regards.

    Comment

    • Dave

      #3
      Re: Assigning to a reference


      "Julián Albo" <JULIANALBO@ter ra.es> wrote in message
      news:3FAA934F.7 E4915BA@terra.e s...
      Dave escribió:
      [color=blue][color=green]
      >> 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[/color][/color]
      only
      [color=blue]
      >References are not assignables, when you assign to one you are really
      >assigning to the object refered by it.[/color]

      Oh that's right; Once a reference is bound to a non-reference variable,
      there's no way to ever rebind it to some other variable, is there???


      Comment

      • John Brown

        #4
        Re: Assigning to a reference

        > Oh that's right; Once a reference is bound to a non-reference variable,[color=blue]
        > there's no way to ever rebind it to some other variable, is there???[/color]

        That's right.


        Comment

        • Andrey Tarasevich

          #5
          Re: Assigning to a reference

          Dave wrote:[color=blue]
          > ...
          > 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;[/color]

          A reference cannot be assigned to. A reference cannot be made to "refer
          to a new object". The value of a reference is determined at the point of
          the reference's initialization. Once the initialization is complete,
          this value cannot be changed. Ever. Moreover, there's no way to access
          this value, you can only access the object the reference is bound to.
          [color=blue]
          > I'm not actually copying
          > an object. Why should operator= come into play?[/color]

          But you _are_ actually trying to copy an object. That's what your code
          means. That's why 'operator=' comes into play.
          [color=blue]
          > After all, I can pass a
          > reference to bar_t as a parameter just fine even though the copy constructor
          > is also inaccessible.[/color]

          Every time you are calling a function you are actually creating and
          _initializing_ its parameters from scratch. Initialization of function
          parameters is just that - initialization. As I said above, at
          initialization stage you can bind a reference wherever you want to bind it.

          What you do inside the function is assignment (as opposed to
          initialization) . At this stage you have no access to the reference's
          value. Whatever you do to 'b' you do to the object 'b' is bound to.

          If you are looking for a "rebindable " way to refer to an object, use a
          pointer, not a reference.

          --
          Best regards,
          Andrey Tarasevich

          Comment

          • Julián Albo

            #6
            Re: Assigning to a reference

            Dave escribió:
            [color=blue]
            > Oh that's right; Once a reference is bound to a non-reference variable,
            > there's no way to ever rebind it to some other variable, is there???[/color]

            Yes. You need a pointer to do that.

            Regards.

            Comment

            Working...