cpp basic but easy confused question
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.
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:
Edit:
My bad. I didn't spot the const in (1) :/
(2) still holds ;)
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 ;)
Quote:Original post by daviddiligent
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.
(2) will allow the function to be invoked on a const instance.
(1) is not legal C++
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').
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
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
Quote:Original post by rip-off
Have 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?
Quote:Original post by daviddiligentQuote:Original post by rip-off
Have 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)
Are you sure you didn't misread?
These are both legal ways of declaring const instances. Furthermore
is a way of declaring a method that accepts a const instance of something. However, your syntax
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement