Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const function

0 views
Skip to first unread message

Jack

unread,
May 18, 2006, 4:30:45 AM5/18/06
to
Would I get compile or link errors if I place or misplace a "const" keyword
in a function declaration/definition when it is not supposed to be there?
(Are const functions are useful in the design process when the function is
believed to be not mutable but as long as the programmer is concerned,
putting such a keyword in the function declaration is optional or it is just
a stuff which is just only by design)
Thanks
Jack


Jack

unread,
May 18, 2006, 4:52:50 AM5/18/06
to

"Jack" <j...@knight.com> 撰寫於郵件新聞:eM9WgVle...@TK2MSFTNGP04.phx.gbl...
somethin' like int getchar() const;
is the keyword "const" optional?
>


Ulrich Eckhardt

unread,
May 18, 2006, 6:16:59 AM5/18/06
to

void func(int); // default
void func(int const); // same as default
void func(int) const; // error

void func2( int*); // default
void func2( int* const); // same as default
void func2( int const*); // overload
void func2( int const* const); // same as overload

struct foo {
void func3(int); // default
void func3(int const); // same as default
void func3(int) const; // overload, won't modify '*this'
void func3(int const) const; // same as overload
};

BTW: the same applies to 'volatile' or 'const volatile'.

Uli

Alex Blekhman

unread,
May 18, 2006, 6:52:09 AM5/18/06
to

`const' modifier is not "design-only" thing. It changes type
of the function. So, these functions are of different types:

struct X
{
void f();
void f() const;
};

If you call const function where non-const is expected, then
you will get compilation errors. I suggest you to read
following section of C++ FAQ:

"[18] Const correctness"
http://www.parashift.com/c++-faq-lite/const-correctness.html


0 new messages