Sign in to follow this  

cpp basic but easy confused question

This topic is 3502 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
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
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
Quote:
Original post by daviddiligent
I 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 cvqualifiers
affect the type of the this pointer (9.3.2). They also affect the function type (8.3.5) of the
member function; a member function declared const is a const member function, a member function
declared volatile is a volatile member function and a member function declared const volatile is
a 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 form
D1 ( parameter-declaration-clause ) cv-qualifier-seqopt exception-specificationopt

Share this post


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


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


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


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


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


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

Share this post


Link to post
Share on other sites

This topic is 3502 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this