473,407 Members | 2,598 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,407 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 1261
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.