# cpp basic but easy confused question

This topic is 3715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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
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
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
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
Have you tried it in a compiler?

##### Share on other sites
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
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
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.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631739
• Total Posts
3001957
×