473,327 Members | 2,112 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,327 software developers and data experts.

reference types

Hi,

Can someone more proficient in the details of the c++ type system
please tell me how I can instantiate the for constesses of references
that I think should be possible:

1. non-constant reference to non-constant value
2. constant reference to non-constant value
3. non-constant reference to constant value
4. constant reference to constant value

const T & seems to behave like the latter, and I could not find any
good reference on this. I would like to be able to use references to
non-const rvalues.

Jun 27 '08 #1
5 1685
Daniel Oberhoff <da*****@phys.ethz.chwrote:
Can someone more proficient in the details of the c++ type system
please tell me how I can instantiate the for constesses of references
that I think should be possible:

1. non-constant reference to non-constant value
2. constant reference to non-constant value
3. non-constant reference to constant value
4. constant reference to constant value

const T & seems to behave like the latter, and I could not find any
good reference on this. I would like to be able to use references to
non-const rvalues.
There is no such thing as a "non-constant" reference, so 1 and 3 above
are impossible.

2. "T&"
4. "const T&"

If you think you need a non-constant reference, use a pointer.
Jun 27 '08 #2
On May 17, 11:17 pm, Daniel Oberhoff <dani...@phys.ethz.chwrote:
Hi,

Can someone more proficient in the details of the c++ type system
please tell me how I can instantiate the for constesses of references
that I think should be possible:

1. non-constant reference to non-constant value
2. constant reference to non-constant value
3. non-constant reference to constant value
4. constant reference to constant value

const T & seems to behave like the latter, and I could not find any
good reference on this. I would like to be able to use references to
non-const rvalues.
Not like a pointer, a reference must refer to something. If the object
is const, the reference must be const qualified. But if the object
isn't const, the reference can be either const or non-const qualified.
Jun 27 '08 #3
On 2008-05-17 17:41:35 +0200, "Daniel T." <da******@earthlink.netsaid:
Daniel Oberhoff <da*****@phys.ethz.chwrote:
>Can someone more proficient in the details of the c++ type system
please tell me how I can instantiate the for constesses of references
that I think should be possible:

1. non-constant reference to non-constant value
2. constant reference to non-constant value
3. non-constant reference to constant value
4. constant reference to constant value

const T & seems to behave like the latter, and I could not find any
good reference on this. I would like to be able to use references to
non-const rvalues.

There is no such thing as a "non-constant" reference, so 1 and 3 above
are impossible.

2. "T&"
4. "const T&"

If you think you need a non-constant reference, use a pointer.
ah, ha! I always thought you could change the referee of a reference,
thus the reference could be non-const referring to several const
referees in its lifetime (this is a strange use of the word referee as
"reffered-to-object", but it feels like proper use of english
construction rules...is it?). but actually reading another post and
thinking about it I realized it is not, since the reference inherits
the operator= from the referee. I suppose I got used to the idea since
I deal with blitz arrays a lot, which use refcounted storage and the
target storage can be changed, which is like changing the referee, but
explicitly using a function call. ah well :). Though while at it: I
know in c++ it is not allowed to (non-const) reference an rvalue,
though there are times when it makes sense to modify and rvalue, while
not wanting to copy it. Though I suppose this would have to involve
refcounting...

Daniel

Jun 27 '08 #4
Daniel Oberhoff <da*****@phys.ethz.chwrote:
"Daniel T." <da******@earthlink.netsaid:
Daniel Oberhoff <da*****@phys.ethz.chwrote:
Can someone more proficient in the details of the c++ type
system please tell me how I can instantiate the for constesses
of references that I think should be possible:
>
1. non-constant reference to non-constant value
2. constant reference to non-constant value
3. non-constant reference to constant value
4. constant reference to constant value
>
const T & seems to behave like the latter, and I could not find
any good reference on this. I would like to be able to use
references to non-const rvalues.
There is no such thing as a "non-constant" reference, so 1 and 3
above are impossible.

2. "T&"
4. "const T&"

If you think you need a non-constant reference, use a pointer.

ah, ha! I always thought you could change the referee of a
reference, thus the reference could be non-const referring to
several const referees in its lifetime (this is a strange use of
the word referee as "reffered-to-object", but it feels like proper
use of english construction rules...is it?).
No. The word you are looking for is "referent". I don't want to sound
too authoritative on this subject though, my wife happens to be an
English major and I asked her. :-)
but actually reading another post and thinking about it I realized
it is not, since the reference inherits the operator= from the
referee.
I understand what you are trying to say here, but strictly technically
what you said is incorrect. The reference doesn't *inherit* the op= from
the referent. It's better to think of the reference as simply another
name for the the referent, an alias, rather than something that is
somehow separate from the referent.
I suppose I got used to the idea since I deal with blitz arrays a
lot, which use refcounted storage and the target storage can be
changed, which is like changing the referee, but explicitly using a
function call. ah well :).
Sounds like a ref-counted smart pointer. Boost has a good implementation
of it if you want to use one. It's also a handy thing for a beginner to
write if he wants to learn more about operator overloading.
Though while at it: I know in c++ it is not allowed to (non-const)
reference an rvalue, though there are times when it makes sense to
modify and rvalue, while not wanting to copy it.
I'm not so sure I agree with the sentiment...
Jun 27 '08 #5
Daniel Oberhoff <da*****@phys.ethz.chwrote in news:4831eade$0$6510
$9*******@newsspool4.arcor-online.net:
[...]
Though while at it: I
know in c++ it is not allowed to (non-const) reference an rvalue,
though there are times when it makes sense to modify and rvalue, while
not wanting to copy it. Though I suppose this would have to involve
refcounting...
Not at all. Calling a non-const member function on a temporary is legal,
and this non-const member function can return a lvalue reference. So one
can turn a rvalue into a lvalue easily, the intent has just be expressed
explicitly:

// pseudocode:
enum MsgType {...}; // message type
class Data {...}; // a "variant" data type.

class GuiMessage {
public:
GuiMessage(MsgType type, Data arg1, Data arg2);
GuiMessage& Ref() {return *this;}
void Swap(GuiMessage& other); // swaps the contents
...
};

void PostToQueue(GuiMessage& message) {
...
message.Swap(to_queue_internals);
...
}
PostToQueue( GuiMessage( msgtype_update_all_views, this, NULL).Ref());
Here, GuiMessage objects are constructed only for posting them into some
message queue. There is no point for keeping the object alive at the call
site so they are constructed as temporaries. For gaining non-const access
for swapping the contents away the Ref() function is used.

C++0x will provide moving constructors and rvalue references, which can
be used to accomplish the same more systematically.

Regards
Paavo
Jun 27 '08 #6

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

Similar topics

6
by: trexim | last post by:
Hi, I am trying to create a Web Reference for CSTA using the URL http://www.ecma-international.org/standards/ecma-348/csta-wsdl/csta-wsdl-all-operations.wsdl Visual .Net complains that: "...
8
by: Nader | last post by:
Hello all, In C# string is a reference type but I learned that string is different from other reference types such as class. For example, if you pass a string argument to a method and then...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
4
by: Edward Diener | last post by:
I have a class Y in assembly B which is derived from a class Z in assembly C. So I correctly add a reference to assembly C in assembly B, build assembly B and everything builds fine. Now I create...
0
by: Kaimar Seljamäe | last post by:
Hi, I have to create a web service client which uses SOAP encoding but does not use "multi-reference" values (see http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383513 item 10). If I...
2
by: Anders Borum | last post by:
Hello! I was looking at marking objects with a changed state, once properties have been changed. The reason behind this is, that I would like to enlist such objects for processing in a...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
9
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.