473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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=(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
5 3634
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

"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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Dave | last post by:
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...
3
by: Ricky W. Hunt | last post by:
I came across something by accident; assigning one object to another (as in object1 = object2) allows me to "work on" object2 just like it was object1. For instance, in order to be able to...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: eBob.com | last post by:
I know that this must be a really dumb question but I just can't find an answer. I want to associate some information with a RichTextBox. The Tag property seems to be the intended way to "hang"...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
1
by: speralta | last post by:
For some reason the text in h2 tag is displaying as white in IE. http://www.salperalta.com/ <td class="sidebar" id="sidebar-right"> <div class="block block-listing" id="block-listing-0">...
7
by: Daniel | last post by:
I am baffled. According to the C++ faq lite it is ok to use a reference as an lvalue, and the subscript operator returns a reference. However, when I run this program, it crashes! I will go set up...
8
by: getmeidea | last post by:
Hi, I am using JDK 1.5. I have a program like this. Here i am directly assigning value to one object. It does'nt give me any compile time or run time error. In java we dont have access to any...
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.