By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

return type function specifier

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
=?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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.