-1

This is an example of a function taking const char * & as a parameter.

#include <iostream>
using namespace std;

char test[] = "Test";

void func(const char * & str)
{
    str = &test[0];
}

int main() {
    const char * mytest;

    func(mytest);

    cout << mytest << endl;

    return 0;
}

Why does this work? (http://ideone.com/7NwmYd)

What const means here? Why func() can change str given to this function?

Upd. It's a newbie question, please not minus it.

vladon
  • 8,158
  • 2
  • 47
  • 91

1 Answers1

3

cost refers to the portion of the declaration immediately to its left, except in the special case that const is first, in which case immediately to its right:

const char * &  // the char is const, the * is not
char const * &  // the char is const, the * is not
char * const &  // the * is const, the char is not
char const * const &  // the char and * are both const

You had a non const pointer to a const char and modified the pointer (not the char) so there was nothing for the compiler to complain about.

JSF
  • 5,281
  • 1
  • 13
  • 20