By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Assigning to a reference

P: n/a
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;
};
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Jul 19 '05 #2

P: n/a

"Julián Albo" <JU********@terra.es> wrote in message
news:3F***************@terra.es...
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.


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???
Jul 19 '05 #3

P: n/a
> 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.
Jul 19 '05 #4

P: n/a
Dave wrote:
...
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.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #5

P: n/a
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.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.