Questions about defining a funciton
-
Hello All, Can anyone tell me that what is the different when declare function like const char* f(); and char* const f(); I would like to know what does this two function signature means. Thanks! :)Nachi
easy, the second don't exist ! lol in fact, you might want to say
const char* f();
char* f const();that's simple. the first one returns a
const char*
when the second one returns a simple char*. What does the secondconst
mean, you'll ask me !? In fact, that is used in object programming, when you read values in an object, where you don't alterate that object... In brief, it means that you don't modify the members of your objet... you don't need this in pure C so... see you ;)TOXCCT >>> GEII power
-
Hello All, Can anyone tell me that what is the different when declare function like const char* f(); and char* const f(); I would like to know what does this two function signature means. Thanks! :)Nachi
-
easy, the second don't exist ! lol in fact, you might want to say
const char* f();
char* f const();that's simple. the first one returns a
const char*
when the second one returns a simple char*. What does the secondconst
mean, you'll ask me !? In fact, that is used in object programming, when you read values in an object, where you don't alterate that object... In brief, it means that you don't modify the members of your objet... you don't need this in pure C so... see you ;)TOXCCT >>> GEII power
Ummm, You said the second one char* const f(); does not exist, but if I write this in Visual C++, the compiler does not complain. Also, for the first one, I can actualy return something which is not a const, eg, const int f() { int i = 10; return i; } and it also works in Visual C++ too. And would you mind to tell me why's that? Thank you very much! ;) Nachi
-
-
Ummm, You said the second one char* const f(); does not exist, but if I write this in Visual C++, the compiler does not complain. Also, for the first one, I can actualy return something which is not a const, eg, const int f() { int i = 10; return i; } and it also works in Visual C++ too. And would you mind to tell me why's that? Thank you very much! ;) Nachi
Stop playing silly games in here, returning a const value doesn't make any sense! Don't try it, just do it! ;-)
-
Sorry, it doesn't really help, I know how to use a constant data in a program, but I have some confussion when using in the definition of functions Thanks! Nachi
So doesn't it make more sense to continue that thread rather than creating a new one of the same topic? In any case, MSDN has this to say about
const
: When modifying a data declaration, the const keyword specifies that the object or variable is not modifiable. When following a member function's parameter list, the const keyword specifies that the function doesn't modify the object for which it is invoked. With C++, const is often used in place of the #define preprocessor directive. Values defined with const are subject to type checking, and can be used in place of constant expressions. When const is used with pointers, it specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter. So,const char *cpch
means that the object pointed to by the pointer is const. And,char *const pchc
means that the value of the pointer — the actual address stored in the pointer — is const.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Stop playing silly games in here, returning a const value doesn't make any sense! Don't try it, just do it! ;-)
Hello, You are right, I know that it does not make any sense, but I still don't know what is this, int const f(); even thought I never use it in my programming experience. But in some C++ test, people ask something about this, and what I know and what I have used is only something like this, void f(const int); or void f(int) const; and these both make sense to me, but not int const f(); therefore, the's why I would like to know whether this signature make sense to any of you. But anyway, thank you for all your suggestio! Nachi
-
Hello All, Can anyone tell me that what is the different when declare function like const char* f(); and char* const f(); I would like to know what does this two function signature means. Thanks! :)Nachi
const char*
means "pointer to a constantchar
" - you cannot change the value of thechar
that is pointed to, but you can change the pointer to point at some otherchar
.char* const
means "constant pointer to achar
" - you can change the value of thechar
pointed to, but you cannot change the pointer to point a some otherchar
. Andconst char* const
means a combination of the two. :) Example code:char c = 'c';
char d = 'd';
const char* p1 = &c;
char* const p2 = &c;*p1 = 'x'; // illegal
p1 = &d; // legal*p2 = 'z'; // legal
p2 = &d; // illegal--Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Laugh it up, fuzzball.