473,473 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

return type function specifier

Hi,

does the return type specifier const for functions for built-in types
ever make sense?

Like in:

const void DoSth();

const int DoSth();

Greets,

PsiX

Sep 23 '06 #1
5 1897
On 23 Sep 2006 12:12:50 -0700 in comp.lang.c++, "PsiX"
<go****@psix.dewrote,
>Hi,

does the return type specifier const for functions for built-in types
ever make sense?

Like in:

const void DoSth();

const int DoSth();
No, it makes no sense at all, and even less for 'void'.

Sep 24 '06 #2
PsiX posted:
const int DoSth();

Firstly, it has no effect if you apply it to a built-in type, because return-
by-value results in an R-value in anyway (which can't be assigned to).
If applied to a user-defined type, you won't be able to invoke any non-
const member functions:

Func().SomeNonConstMemberFunc(); /* Compiler ERROR */

I myself use const return values to serve as a reminder to the programmer
that he/she should keep track of the return value (e.g. for dynamic memory
allocation):

char *const Str()
{
char *const p = new char[30];

strcpy(p,"Monday the 5th of August");

return p;
}

--

Frederick Gotham
Sep 24 '06 #3
Frederick Gotham wrote:
I myself use const return values to serve as a reminder to the programmer
that he/she should keep track of the return value (e.g. for dynamic memory
allocation):

char *const Str()
{
char *const p = new char[30];

strcpy(p,"Monday the 5th of August");

return p;
}
And someone in the world understand your intention?

--
Salu2
Sep 24 '06 #4
=?ISO-8859-15?Q?Juli=E1n?= Albo posted:
>I myself use const return values to serve as a reminder to the programmer
that he/she should keep track of the return value (e.g. for dynamic memory
allocation):

char *const Str()
{
char *const p = new char[30];

strcpy(p,"Monday the 5th of August");

return p;
}

And someone in the world understand your intention?

They understand the meaning of "const".

The two following signatures are effectively equivalent:

(1) char *const Str(void)

(2) char * /* Keep track! */ Str(void)

--

Frederick Gotham
Sep 24 '06 #5
Frederick Gotham wrote:
>>I myself use const return values to serve as a reminder to the
programmer that he/she should keep track of the return value (e.g. for
dynamic memory allocation):

char *const Str()
{
char *const p = new char[30];

strcpy(p,"Monday the 5th of August");

return p;
}

And someone in the world understand your intention?

They understand the meaning of "const".
Yes.

--
Salu2
Sep 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Busin | last post by:
Classes B, C, D and E are derived from base class A. A* CreateB(); A* CreateC(); A* CreateD(); A* CreateE(); class X { public:
52
by: Robert | last post by:
Alright, here's my situation: if i use malloc like: char *mystring; mystring = (char *)malloc(80); return mystring; free(mystring);
31
by: CeZaR | last post by:
Hi, How can i specify the return type of a function returning a managed array of chars. If i try to write: "char __gc func()" i get an error! How can i do that? Thanks!
6
by: alternativa | last post by:
Hi, I have problem with the following function - it was intended to ask a user for a 4-digits number: double ask_for_number (void) { char *notint; char s2; double entered_number;
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
9
by: Christian Christmann | last post by:
Hi, do I have to provide the return type when a function is declared in ANSI C-99? Or would main ( void ) { ... } be enough?
25
by: Christian Christmann | last post by:
Hi, the ANSI-C 99 standard specifies that the main function has "int" as return type. However, there are still lots of people declaring "void" as main return type. Did previous ANSI-C...
0
by: Ole Nielsby | last post by:
The C++ syntax goes like: type-id: type-specifier-seq abstract-declarator(opt) type-specifier-seq: type-specifier type-specifier-seq(opt) which means, a type-id has 1 or more...
7
by: asit | last post by:
#include <stdio.h> //#include <stdlib.h> int main() { int c; printf("c before call=%d\n",c); c=message(); printf("c after call=%d\n",c); return 0;
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...
0
agi2029
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.