473,327 Members | 2,055 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.

Need help c++ reference.

9
Expand|Select|Wrap|Line Numbers
  1. int main(){
  2.     char const &rr = 'x';     //works
  3.     //const char &rr = 'x';       works
  4.  
  5.     char a[5] = "Hi!";
  6.     char* const &r = a;     //works
  7.     //const char*  &r = a;      doesn't works
  8.  
  9.     std::cout << rr << std::endl;
  10.     std::cout << r[2] << std::endl;
  11.     system("pause");
  12.     return 0;
  13. }
Sep 14 '14 #1
3 1257
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. char a[5] = "Hi!";
defines a as the address of a[0]. Therefore, a is a char*.

This code:
Expand|Select|Wrap|Line Numbers
  1. //const char*  &r = a;      doesn't works
  2.  
attempts to define r as a reference to a variable that is a const char* The variable a is not const and so you can't use it to create a reference that is const.

Remember, in C++, that a reference is just another name for an already existing variable. Because of this, you can't have the reference const and the variable not const. The reference and the variable are the same variable.
Sep 14 '14 #2
carbon
9
I have cleared my doubts.

Cv-qualifiers like const apply to whatever is to the left of them, unless there is nothing, in which case they apply to the right.

Expand|Select|Wrap|Line Numbers
  1. char* const &r = a;
  2.  
The above code works because here, r has been declared a reference to a constant pointer that is pointing to a character type.


Expand|Select|Wrap|Line Numbers
  1. const char*  &r = a;
The above code doesn't work as here, r has been declared as a reference to a pointer that is pointing to a constant character.

Thanks Anyways ;)
Sep 16 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
So you see now that problem was not with references but with what was const.

It's OK to have a const reference to something not const because if the something changes the const-ness of the reference does not.

But you may not define a reference to a const something if the something is not const. If the something changes, the reference to const is violated.
Sep 16 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: normd | last post by:
I have a text file which contains VB.NET code. The VB.NET code is actually a Class that can contain code, which simply displays a message box or does something more complicated (i.e Excel/Word...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
4
by: thomson | last post by:
Hi All, Probably it may be a dump question, But as iam in a learning Phase, you have the right to ask these questions Why we need Reference Types? What is the advantage of that when compared to...
0
by: kid4rilla | last post by:
I can successfully write the binary data to an image data type, and successfully retrieve it, but when I attempt to play the file in media player after retrieving it, I get the file type isn't...
4
by: Bo Peng | last post by:
Dear list, I am looking for a way to store a large amount of unique sequences that will be accessed by objects. The most important operations are: 1. Direct access to the sequences (from...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
0
by: Elmo Watson | last post by:
I have a linkbutton in one gridview column - I also have another column with a label on it - lblID the linkbutton has a redirect, but I need a reference to the current row it's on (VB.Net), so I...
2
by: Grizlyk | last post by:
Hello. Can compiler garantee equal optimization in the following example for reference "named_ref" as for "named_function"? How I can declare that the "named_ref" always will returns "*this"? ...
4
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string,...
10
by: not_a_commie | last post by:
I've seen studies before showing that it is better to rewrite code when more than 25% (or whatever) of the code needs to be changed. I can't seem to locate any references for that at the moment. Do...
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...
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: 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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.