473,791 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
};
Jul 19 '05 #1
5 3651
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********@ter ra.es> wrote in message
news:3F******** *******@terra.e s...
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
3978
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 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).
3
1672
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 reference a textbox on form1 I created a textbox at module level called PassTextBox and assigned the original textbox to it (textbox1 = PassTextBox) in Form1's load code. I was planning on just using this as a "hold" area and when I got back into Form1 I...
20
7013
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
2
1373
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" some additional information on a Control. I've created a structure, rtbAuxInfo, for the information. I think that I have found the right syntax for assigning a pointer to the structure to the Tag property of the RichTextBox, but I can't figure...
7
2533
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 several methods that are working fine. The problem is if I want to dynamically assign an event handler to the object, the event handler is not called. What am I doing wrong? Below is a sample HTML doc with everything in place. <!DOCTYPE HTML PUBLIC...
1
1765
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"> <h2>Easy MLS Search for Oregon and Southwest Washington:</h2> <div class="content"> </div> </div>
7
1762
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 a different compiler and try again while I'm waiting for a reply, but I'm really curious about why this is happening. I'm using the mingw compiler that came with dev-c++ on an up to date Windows 2000 box. Any help would be greatly appreciated,...
8
6239
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 object reference. Still this kind of assignment is allowed. What is the reason behind this. Class Sample{ public static void main(){ //Assigning value to object reference itself. Integer i = 10;
8
4547
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 "this" to the result, but I always get the error message, "Cannot assign to '<this>' because it is read-only." I've been searching on the Internet, and I notice some C# code is violating this rule. Perhaps their code is wrong.
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.