Re: const function() !!??

From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 12/30/04


Date: Thu, 30 Dec 2004 10:46:29 -0800

Sundar wrote:

> Do we have something called a const function ? I thought applying the
> 'const' specifier to a function is meaningless...

You can't apply the 'const' specifier to a function in C. That would be
a syntax error.

> but when i tried
> defining a const function it worked !!
> like...
> const int func(int a)
> {
> int b;
> .....
> return b;
> }

This is not a 'const' function. In accordance with C syntax, this
'const' qualifier is applied to the return type of the function. I.e.
this function return type is 'const int'.

Syntactically, a naive attempt to apply a 'const' qualifier to a
function would look as follows

  int (const func)(int a)

but this simply won't compile.

-- 
Best regards,
Andrey Tarasevich


Relevant Pages

  • Re: switch statement in C
    ... const int state_black = 1; ... Because the variable is still a variable, and not a compile-time constant. ... As question 11.8 in the FAQ explains, the "const" qualifier really ...
    (comp.lang.c)
  • 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: const bei Parametern egal?
    ... void bar (const int i); ... hatte ich eigentlich eine Compiler-Fehlermeldung wegen des fehlenden const in der Definition von foo::barerwartet. ... the const and volatile typespecifiers ... for each parameter type are ignored ...
    (microsoft.public.de.vc)
  • 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)