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

Home Posts Topics Members FAQ

Assigning to references

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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05
37 3980
On Thu, 06 Nov 2003 20:26:30 -0500, Dave wrote:
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;}
class foo_t {
public:
foo_t(bar_t *b);
void set_bar(bar_t *b) {bar = b;}
void set_bar(bar_t &b) {bar = &b;}
private:
bar_t *bar;
}

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.
You are calling the assignment operator, when you do "bar = b". The copy
constructor has nothing to do with it.
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 Logger {
public:
Logger();

void setStream(ostre am &stream);

ostream& stream();
ostream& operator << (...);
private:
ostream *myStream;
}

....
Logger log(std::cout);

log.stream() << "blah\n";

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #11
"Dave" <be***********@ yahoo.com> wrote in message news:<vq******* *****@news.supe rnews.com>...
Hello all,

Hi Dave,

The answer is short and harsh I'm afraid. The Standard, (8.5.3-2),
states:

"A reference can not be changed to refer to another object
after initialization. "

and this is exactly what you have hit upon. The compiler is happy
initialising
your reference in the ctor of foo_t, but after that it will not allow
bar to change. So, when you say

bar = b

the compiler resolves this to

deref(bar)->operator=( b );

which fails since operator= is private.

-=jarl

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #12

"Dave" <be***********@ yahoo.com> wrote in message
news:vq******** ****@news.super news.com...
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
You Can't Change References. Once it has been constructed, a reference
behaves exactly like the object it refers to (it is an "alias" for that
object). Assigning to it is exactly the same as assigning to the original
object. Consider this code:

int a = 1;
int& b = a; // b refers to a
b = 2; // Now a == 2. "b" is simply an alias for "a".
int& c; // This is an error. You can't have a reference that doesn't
refer to anything.
int& d = b; // d refers to b, which is the same as a
b = d; // Exactly equivalent to "a = a".

Contrast this with the behaviour of pointers:

int a = 1;
int* b = &a; // b points to a
*b = 2; // Now a == 2.
int c = 3;
int* d = &c; // d points to c
b = d; // b points to whatever d points to (i.e. c)
c = 4;
assert(*b == 4);
// Note the contrast of *b, meaning the object b points to, and b, the
pointer itself.
// References make no such distinction; you are always referring to the
referenced object.

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?

What you probably meant to do is have the member bar be a *pointer*.

For example:

class foo_t
{
bar_t* bar;
public:
foo_t(): bar(0) { }
void set_bar(bar_t& b) { bar = &b; }
void do_something() { bar->do_something() ; }
};

Bear in mind that, unlike a reference, a pointer will not extend the
lifetime of the object it points to. So if you were to write code like
this:

void give_me_a_bar(f oo_t& foo)
{
bar_t bar;
foo.set_bar(bar );
}

int main()
{
foo_t foo;
give_me_a_bar(f oo);
foo.do_somethin g(); // crash here
}

then the Result Is Undefined.

Regards
David Turner

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #13
Dave wrote:
<snip>
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?

<snip>

You misunderstand what a reference is. Assigning to a reference
means assigning to the object it refers to, not binding the
reference to another object.

You need to store a pointer instead, and use unary * and &
operators to convert between pointers and references.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #14
In article <vq************ @news.supernews .com>, Dave
<be***********@ yahoo.com> writes
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.


You have misunderstood what a reference is in C++. It is purely a name
that is bound to an existing object and the language does not provide a
normal mechanism for rebinding the name to a different object.
operator=() deals with objects and values not with names so bar = b
requires that the value of b be assigned to the object referred to by
bar. As there is no operator=() for the type in question this cannot be
done.

The special property of reference parameters is that binding between
name and object takes place as part of the process of calling the
function so that parameter name can be bound to many different objects,
but each time that binding is for the duration of the function call.

The best you can do is to explicitly change any publicly writable
characteristics of the object:

void change_bar(bar_ t& b){
bar.write_val1( b.read_val1());
bar.write_val2( b.read_val2());
etc.
}

But be very careful if your process is multi-threaded because it seems
that bar is a global variable and so the above process must be protected
by something such as a mutex.
--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #15
"Dave" <be***********@ yahoo.com> wrote in message news:<vq******* *****@news.supe rnews.com>...
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.


You cannot reset a reference. The instruction above tries to assign
directly to bar. The solution is to define bar as a pointer to a bar_t
and change set_bar() to

void set_bar(bar_t &b) {bar = &b;}

Cheers,
Nicola Musatti

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #16
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave wrote:

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

You never assign *to* references, you assign *through* references.

A reference is a different name for an object, and must be bound
when created, and can never be rebound.

References are second class citizens in the C++ world.

You cannot have references to references, you cannot have pointers
to references, if you take the address of a reference (operator&)
you get the address of the referent that the reference is bound
to, etc...

If you're familiar with Pascal, a reference is like adding the
keyword "var" in front of a parameter in a parameter list.

procedure foo(a: integer); begin ... end;
procedure bar(var a: integer); begin ... end;

In C++, we would say:

void foo(int a) { ... }
void bar(int &a) { ... }

In either case, the name "a" in procedure "bar" is, for
all practical purposes, an int-typed variable; it just happens
to be stored differently from the "a" in "foo".

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/q7WOJJVk0d/xqeYRAonUAJ9alY AEtsPy03NSZu+ne 59JT2efBACgiPUN
VhJt7JCc1kTXFEK hU6CNTks=
=ZDoM
-----END PGP SIGNATURE-----

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #17
On Thu, 06 Nov 2003 20:26:30 -0500, Dave wrote:
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;}


C++ references are just aliases to existing objects; they cannot be
reseated. Try using a pointer instead, since that has the meaning you are
after here.

HTH,

Simon Bone
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #18
Ron
> 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.

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


The compiler accepts the line

foo_t(): bar(initial_bar ) {}

-- even though bar_t has no accessible copy constructor -- because
this line binds the reference "bar" to the member variable
"initial_ba r". The line

void set_bar(bar_t &b) {bar = b;}

fails to compile because it's asking the compiler to copy the object
"b" into the object to which the reference "bar" is bound.

Huh? That's right: references can be bound only once. After they're
bound, they refer to the same object forever*. Section 8.5.3(2):

A reference cannot be changed to refer to another object after
initialization. Note that initialization of a reference
is treated very differently from assignment to it....

Thus:

int i;
int &r = i; // r is now an alias for i
r = 3; // i now == 3. r DOES NOT refer to the rvalue "3".
r = 6; // i now == 6

-Ron

* Putting aside nasssty treacherous rebinding tricks using explicit
destructor calls and placement new.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #19

"Dave" <be***********@ yahoo.com> wrote in message
news:vq******** ****@news.super news.com...
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;
};


Hello all,

I had sent an earlier post that for some reason didn't get through (and some
of you may have noticed that for some reason, my original post got posted
twice; sorry about that...). So, I'm having some trouble here. In any
case, I just wanted to thank everyone for all of the responses. I can't
believe I had such a massive mental lapse! Your points are very well made
and very well taken!

As long as I'm writing, I'll make one more query. This is purely out of
curiosity - I'm not trying to do anything specific. And of course, if there
*is* a way to do what I'm about to ask about, it would no doubt be very
ill-advised so, again, this is purely out of curiosity...

One poster mentioned the possibility of re-binding a reference by destroying
the old rerferent with a manual destructor call and then using placement new
to construct the new object in the same memory. Though ill-advised, this
does sound like a standard-conforming way to do it. This got me to
wondering...

Is there some sort of treacherous trick that would allow us to get the
address of the reference (as opposed to the referent)? Is a reference even
guaranteed to *have* an address, or might it be just a simple compile-time
alias for the referent? I'd be interested to here any thoughts on all of
this...

Thanks again,
Dave
Jul 19 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6411
by: gtz669 | last post by:
I have a class which I am using for data stroage. I declare an instance of that class in my main class which is running my java applet. I Iassign it a value in the init () function and it works fine but when I try to do the same assignment later on, it restults in a NullPointerException and I can not figure this out. Could someone please help. Thanks. Here's the code: public class A extends java.applet.Applet
5
3652
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).
14
74902
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0; myarray=0; myarray=1; myarray=8;
8
13722
by: Woody Splawn | last post by:
Lets say I have a winform that is populated with a dataset. The dataset and data table may have several rows in it. Lets say I am looking at the winform and I want to assign a value to a certain column in the associated datatable. Lets say there are 10 rows in the table and I am on row 5, and I want to assign the value to row 5, but I don't know that I am on row 5. Anyway, my method for assigning the value to the field would be: Dim Dt...
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
2584
by: Bernie Hunt | last post by:
I'm having trouble assigning the reportsource value at run time. I'm running a test setup, one form with only a Crystal Viewer, called cvwMail. My report is called CustomersBasic.rpt and it draws directly from the TradeWinds.MDB test database. If I use Me.cvwMain.ReportSource = "J:\Learning\CMP 214\Crystal Test\CrystalTest \CustomersBasic.rpt to point to the file directly it works.
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
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.
10
4025
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
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
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10156
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
9997
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...
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
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
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...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.