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