return type function specifier

P

PsiX

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
 
D

David Harmon

On 23 Sep 2006 12:12:50 -0700 in comp.lang.c++, "PsiX"
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'.
 
F

Frederick Gotham

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;
}
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Frederick said:
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?
 
F

Frederick Gotham

=?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)
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Frederick said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top