0

If I can accept an char variable as const char& in a function, Why can't I accept a char* as a const char *& ? What I want to do is to have a reference to a pointer with which I am not going to change the data to which it point to, But, to change the location to which the pointer points.

sajas
  • 1,599
  • 1
  • 17
  • 39
  • 3
    Simple example code would be useful, with the corresponding error messages – lxop Mar 13 '13 at 05:26
  • Why not using simple const char * instead? – Subhajit Mar 13 '13 at 05:45
  • 2
    One thing to note, in differentiating your 2 cases: In `const char&`, the `const` is a top level const. That is to say, it applies to the whole type, which is char. In `const char*&`, the const is not a top level const. It doesn't apply to the whole type, which is `char*`, it only applies to the `char`. A top level const in that case would look like this: `char* const&`. Consequently, if your parameter type was `char* const&`, it would accept a `char*` as an argument. – Benjamin Lindley Mar 13 '13 at 05:46
  • @BenjaminLindley I think your statements are misleading because references are always "top level const" because they cannot be rebound. There is no reason to view `const` in `const char&` any differently from tha in `const char *`. In both cases `const` apples directly to the `char` part of the declaration. The `const` in `const char &` is not "top level" as can be seen by the fact that `void f(const char&)` and `void f(char&)` are not equivalent function declarations. Of course, an explicit top level `const` on a reference type would be meaningless and is not allowed. – CB Bailey Mar 13 '13 at 06:28
  • @CharlesBailey: The difference is not between `const char&` and `const char*`, it is between `const char&` and `const char*&`. There is definitely a reason to view the const differently there. But perhaps my comments were misleading, because I was looking at things from the point of view of the function accepting the argument. Let me restate it. – Benjamin Lindley Mar 13 '13 at 06:31
  • @BenjaminLindley: OK, I think I understand. The issue is really about the type being bound, not the reference type itself but as you kept the `&` in your comment while talking about "top level const", I found it somewhat confusing. – CB Bailey Mar 13 '13 at 06:46
  • @CharlesBailey: Okay, good. I was having a tough time rephrasing it sensibly, now I don't have to. – Benjamin Lindley Mar 13 '13 at 06:47

0 Answers0