Re: const bei Parametern egal?



Claus Henning schrieb/wrote:

class foo
{
void bar (const int i);
};

void foo::bar (int i)
{
}

hatte ich eigentlich eine Compiler-Fehlermeldung wegen des fehlenden const in der Definition von foo::bar() erwartet.

Nein, das ist so in Ordnung. Die entsprechende Stelle im Standard ist:

13.2 Declaration matching

[...]

— Parameter declarations that differ only in the presence or absence of const and/or volatile are
equivalent. That is, the const and volatile typespecifiers
for each parameter type are ignored
when determining which function is being declared, defined, or called. [Example:
typedef const int cInt;
int f (int);
int f (const int); // redeclaration of f(int)
int f (int) { ... } // definition of f(int)
int f (cInt) { ... } // error: redefinition of f(int)
—end example]

Only the const and volatile typespecifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile typespecifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations.112) In particular, for any type T, “pointer to T,” “pointer to const T,” and “pointer to volatile T” are considered distinct parameter types, as are “reference to T,” “reference to const T,” and “reference to volatile T.”

112) When a parameter type includes a function type, such as in the case of a parameter type that is a pointer to function, the const and volatile typespecifiers at the outermost level of the parameter type specifications for the inner function type are also ignored.
.



Relevant Pages

  • Re: Is this legal?
    ... That it is the only way to initialise an 'const' ... | online Comeau snippet compiler. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: accessor member functions and const
    ... I want to be able to reference the returned ... const int& getValconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: problem about const member in a struct
    ... if I declared a struct like following: ... the compiler ascertain the value of the const member? ... const int birth_year; ...
    (comp.lang.c)
  • Re: const int value changing....
    ... > when a 'const int ' is declared, where is it been created ... What is RAM? ... declaring a variable does not ...
    (comp.lang.c)
  • Re: const int value changing....
    ... > when a 'const int ' is declared, where is it been created(in RAM?) ... the compiler won't allow you to modify a const variable. ...
    (comp.lang.c)