Jump to content
  • Advertisement
Sign in to follow this  
daviddiligent

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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++

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by daviddiligent
I just saw an example

Can we see it?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by daviddiligent
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?

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.cpp
1>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-int
1>e:\dev\temp\main.cpp(4) : error C2062: type 'int' unexpected
1>e:\dev\temp\main.cpp(5) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>Temp - 4 error(s), 0 warning(s)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!