# cpp basic but easy confused question

## Recommended Posts

What's the difference between the following two functions: returnType functionName const(arguments); //(1) and returnType functionName (arguments) const; //(2) I know the (1) will allow the const instances to invoke the function. What does the (2) mean? Are these two functions have to be defined (or decleared) inside of a class? Thanks in advance.

##### Share on other sites
Wan    1366
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10

In short, you promise that calling the function keeps the state of the class instance unchanged. For instance, you'll often see this in simple property get-functions:

class Enemy{private:  int health;public:  // correct  int getHealth() const  {    return health;  }  // wrong  void kill() const  {    health = 0;  }};

Edit:
My bad. I didn't spot the const in (1) :/
(2) still holds ;)

##### Share on other sites
Nitage    1107
Quote:
 Original post by daviddiligentWhat's the difference between the following two functions:returnType functionName const(arguments); //(1)and returnType functionName (arguments) const; //(2)I know the (1) will allow the const instances to invoke the function. What does the (2) mean? Are these two functions have to be defined (or decleared) inside of a class? Thanks in advance.

(2) will allow the function to be invoked on a const instance.
(1) is not legal C++

##### Share on other sites
jpetrie    13106
As above, (1) is illegal. (2) means that the 'this' pointer is of type 'const T* const' rather than just 'T* const' (e.g., you can invoke the method on instances of 'const T').

##### Share on other sites
Thanks for WanMaster's reply. I remember the usage of (2).

But, I am sure that Both "jpetrie" and "Nitage" are wrong, because I just saw an example explaining that the (1) is for the use of const instances. And it is also legal. The usage of (2) is like what WanMaster's explanation

##### Share on other sites
rip-off    10976
Have you tried it in a compiler?

##### Share on other sites
Wan    1366
Quote:
 Original post by daviddiligentI just saw an example

Can we see it?

##### Share on other sites
Quote:
 Original post by rip-offHave you tried it in a compiler?

I saw it in a book. So I believe it's right. I can't try it now. Can you try it?

##### Share on other sites
Wan    1366
Quote:
Original post by daviddiligent
Quote:
 Original post by rip-offHave you tried it in a compiler?

I saw it in a book. So I believe it's right. I can't try it now. Can you try it?

We could, but we already know what's going to happen. I think you need a new book.

Edit: O, what the heck...

MSVC:Compiling...1>Main.cpp1>e:\dev\temp\main.cpp(4) : error C2143: syntax error : missing ';' before 'const'1>e:\dev\temp\main.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int1>e:\dev\temp\main.cpp(4) : error C2062: type 'int' unexpected1>e:\dev\temp\main.cpp(5) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body1>Temp - 4 error(s), 0 warning(s)

##### Share on other sites
jpetrie    13106
Are you sure you didn't misread?
const int x = ...;int const x = ...;

These are both legal ways of declaring const instances. Furthermore
int method(const T ... );

is a way of declaring a method that accepts a const instance of something. However, your syntax
int method const ( ... );

isn't legal standard C++. See section 8.4.2 of the standard, where it states that the post-return-type portion of the function declaration must look like, basically, "name ( parameters ) optional-cv-qualifier optional-exception-specifier." It's not legal to have the cv-qualifier (const, in this case) between the name and the parameters.

##### Share on other sites
dmail    116
Quote:
 Original post by daviddiligentI saw it in a book. So I believe it's right. I can't try it now. Can you try it?

I read the other week in the daily sport that Elvis was alive and on the moon :) Was the author Herbert Schildt by any chance?
Quote:
 A nonstatic member function may be declared const, volatile, or const volatile. These cvqualifiersaffect the type of the this pointer (9.3.2). They also affect the function type (8.3.5) of themember function; a member function declared const is a const member function, a member functiondeclared volatile is a volatile member function and a member function declared const volatile isa const volatile member function. [Example:struct X {void g() const;void h() const volatile;};X::g is a const member function and X::h is a const volatile member function. ]

edit
hmmm I had intended on posting the extract that jpetrie mentions but there does not seem any point, what the heck
Quote:
 The declarator in a function-definition shall have the formD1 ( parameter-declaration-clause ) cv-qualifier-seqopt exception-specificationopt

##### Share on other sites
OK, guys.

I am sure I saw it last night from the book called "programming with c++"(name might not be exact).

It mentioned like this: put the const between the function name and the argument list will let the const instances to access these functions.

The book is definitely very old. It might be wrong. But, I am sure I really did see it.

##### Share on other sites
jpetrie    13106
It's definitely wrong then. Could be an unfortunate copyedit error that the publisher didn't catch, or they could be writing an old (pre-standard) version of C++. Either way, example (1) in your original post is not legal modern C++.

It may be time to find a new book.

##### Share on other sites
Quote:
 Original post by jpetrieIt's definitely wrong then. Could be an unfortunate copyedit error that the publisher didn't catch, or they could be writing an old (pre-standard) version of C++. Either way, example (1) in your original post is not legal modern C++.It may be time to find a new book.

Thanks mate. I thought the book is really good, coz I learned a lot from it. Like I always confused about c string and cpp string. It did have two separate chapters talking about them. But on the hand, I did find some other mistakes though when I read it, which is bad.

##### Share on other sites
Nitage    1107
That's pretty much the only place in a member funciton declaration where the keyword const is illegal though.

These are all legal:
int foo()const
int const foo()
int const foo()const
const int foo()
const int foo()const

And there are even more options when the return type is a pointer.

Quote:
 I thought the book is really good, coz I learned a lot from it. Like I always confused about c string and cpp string. It did have two separate chapters talking about them. But on the hand, I did find some other mistakes though when I read it, which is bad.

The C++ FAQ lite, which WanMaster linked to earlier, is an excellent - and accurate - C++ reference.

##### Share on other sites
OK guys. I just checked my book..... I need to announce that the book isn't wrong, although it was published in 2000. I admit that I was wrong. I misread the book.

In the book, it mentioned : A function is declared constant by inserting the const keyword between its parameter list and its body....... The function definition will allow it to be called for constant objects.

What I remembered is "between its parameter list and its function name".

Sigh..... there is so much that I need to learn.

Really Thanks for all who took attention for this post.