Assigning to a reference

D

Dave

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

=?iso-8859-1?Q?Juli=E1n?= Albo

Dave escribió:
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

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

Dave

Dave escribió:
only

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

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

John Brown

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

That's right.
 
A

Andrey Tarasevich

Dave said:
...
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;

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.
I'm not actually copying
an object. Why should operator= come into play?

But you _are_ actually trying to copy an object. That's what your code
means. That's why 'operator=' comes 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.

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

=?iso-8859-1?Q?Juli=E1n?= Albo

Dave escribió:
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???

Yes. You need a pointer to do that.

Regards.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top