Do we have something called a const function ? I thought applying the
'const' specifier to a function is meaningless... but when i tried
defining a const function it worked !!
like...
const int func(int a)
{
int b;
.....
return b;
}
...The above code gave no compilation errors ! :->
How should i interpret it ?
(i used MS- vc++ compiler)
Sundar 27 2076
"Sundar" <su**********@y ahoo.com> wrote in message
news:9e******** *************** ***@posting.goo gle.com...
<snip> const int func(int a) { int b; ..... return b; }
...The above code gave no compilation errors ! :->
And it shouldn't.
How should i interpret it ?
A function returning a const int, that is an non-modifyable integer.
Sundar wrote on 30/12/04 : Do we have something called a const function ? I thought applying the
No.
'const' specifier to a function is meaningless...
defining a const function it worked !! like... const int func(int a) { int b; ..... return b; }
That 'const' is useless.
This one is useful:
char const *get_ver (void)
{
return "1.2";
}
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Sundar wrote: Do we have something called a const function ? I thought applying the 'const' specifier to a function is meaningless...
You can't apply the 'const' specifier to a function in C. That would be
a syntax error.
but when i tried defining a const function it worked !! like... const int func(int a) { int b; ..... return b; }
This is not a 'const' function. In accordance with C syntax, this
'const' qualifier is applied to the return type of the function. I.e.
this function return type is 'const int'.
Syntactically, a naive attempt to apply a 'const' qualifier to a
function would look as follows
int (const func)(int a)
but this simply won't compile.
--
Best regards,
Andrey Tarasevich
"dandelion" <da*******@mead ow.net> writes: "Sundar" <su**********@y ahoo.com> wrote in message news:9e******** *************** ***@posting.goo gle.com...
<snip> const int func(int a) { int b; ..... return b; }
...The above code gave no compilation errors ! :->
And it shouldn't.
How should i interpret it ?
A function returning a const int, that is an non-modifyable integer.
But you can't modify the result of a function anyway.
Is there any context in which there's a difference between
const int func(int a) { /* blah blah */ }
and
int func(int a) { /* blah blah */ }
?
As far as I can tell there isn't, and gcc warns:
tmp.c:3: warning: type qualifiers ignored on function return type
I suppose pointers to the two function types would be incompatible,
but I can't think of any other difference.
Probably it was just easier to allow it in the language definition,
even though it's meaningless, than to disallow it.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Thanks !
I understand 'const' function is meaningless, but please tell me why to
use a 'const' as return type ?!
On 2 Jan 2005 00:45:30 -0800, "__MMS__" <su**********@y ahoo.com> wrote: Thanks ! I understand 'const' function is meaningless, but please tell me why to use a 'const' as return type ?!
Because the return value of the function would result in undefined
behaviour if it were changed (e.g. returning a string literal).
"__MMS__" <su**********@y ahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. . Thanks ! I understand 'const' function is meaningless, but please tell me why to use a 'const' as return type ?!
You may want to return a const pointer because you don't want the pointed-to
object to be changed.
S
--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
"Stephen Sprunk" <st*****@sprunk .org> writes: "__MMS__" <su**********@y ahoo.com> wrote in message news:11******** **************@ z14g2000cwz.goo glegroups.com.. . Thanks ! I understand 'const' function is meaningless, but please tell me why to use a 'const' as return type ?!
You may want to return a const pointer because you don't want the pointed-to object to be changed.
That's a good reason for returning a pointer-to-const-whatever, but I
think the OP was asking why you would want to return a const-whatever
(where "whatever" itself may or may not be a pointer type). As far as
I can tell, there's no reason to do so.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
__MMS__ wrote: ... I understand 'const' function is meaningless, but please tell me why to use a 'const' as return type ?! ...
There's absolutely no point in doing it in C. Values returned by
functions are rvalues. Const-qualification has no effect on rvalues in C
(in C++ things are different, but that's offtopic here).
--
Best regards,
Andrey Tarasevich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christian Engström |
last post by:
If you have a function that returns something by value, the gcc compiler
(version 3.2.3 on Windows XP with MinGW) converts the returned value
from the type you specify in the code, to the const version of that type.
Is this a bug that is specific to gcc, or is it a flaw in the language
specification that gcc diligently implements? For example, the below
program produces the output
Constant
Mutable
|
by: joe |
last post by:
hi,
after reading some articles and faq,
i want to clarify myself what's correct(conform to standard) and
what's not?
or what should be correct but it isn't simply because compilers don't
support.
(first i compiled them with g++3.x. ERR means compiler will bark,
otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
|
by: johny smith |
last post by:
Can someone please explain to me the difference between these two:
function1( const int a)
function2( int const a)
Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.
moreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
|
by: al |
last post by:
char s = "This string literal";
or
char *s= "This string literal";
Both define a string literal. Both suppose to be read-only and not to be
modified according to Standard. And both have type of "const char *".
Right? But why does the compiler I am using allow s to be modified, instead
of generating compile error?
|
by: Roger Leigh |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
A lot of functions use const pointer arguments. If I have a non-const
pointer, it is transparently made const when I pass it to the
function, e.g. char * -> const char *. However, this does not appear
to work when I add another level of indirection:
void test1 (char **value)
{}
| |
by: andrew.fabbro |
last post by:
In a different newsgroup, I was told that a function I'd written that
looked like this:
void myfunc (char * somestring_ptr)
should instead be
void myfunc (const char * somestring_ptr)
When I asked why, I was told that it facilitated calling it as:
|
by: Snis Pilbor |
last post by:
Whats the point of making functions which take arguments of a form like
"const char *x"? It appears that this has no effect on the function
actually working and doing its job, ie, if the function doesn't write
to x, then it doesnt seem like the compiler could care less whether I
specify the const part. Quite the opposite, if one uses const
liberally and then later goes back and changes the functions, headaches
will inevitably occur as...
|
by: hzmonte |
last post by:
Correct me if I am wrong, declaring formal parameters of functions as
const, if they should not be/is not changed, has 2 benefits;
1. It tells the program that calls this function that the parameter
will not be changed - so don't worry.
2. It tells the implementor and the maintainer of this function that
the parameter should not be changed inside the function. And it is for
this reason that some people advocate that it is a good idea to...
|
by: grizggg |
last post by:
I have searched and not found an answer to this question. I ran upon
the following statement in a *.cpp file in a member function:
static const char * const pacz_HTMLContentTypeHeader = "Content-Type:
text/html\r\n";
Why is the second const needed and what does it do?
Thanks
|
by: .rhavin grobert |
last post by:
hello;-)
i frequently need the following construction:
ReturnParam § Function() §
{
/...do something.../
someType var § = something;
/...do something.../
return something;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |