return type function specifier

Discussion in 'C++' started by PsiX, Sep 23, 2006.

  1. PsiX

    PsiX Guest

    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
    PsiX, Sep 23, 2006
    #1
    1. Advertising

  2. PsiX

    David Harmon Guest

    On 23 Sep 2006 12:12:50 -0700 in comp.lang.c++, "PsiX"
    <> wrote,
    >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'.
    David Harmon, Sep 24, 2006
    #2
    1. Advertising

  3. 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
    Frederick Gotham, Sep 24, 2006
    #3
  4. 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
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Sep 24, 2006
    #4
  5. =?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
    Frederick Gotham, Sep 24, 2006
    #5
  6. 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
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Sep 24, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    776
    eminemence
    Jan 25, 2006
  2. Diego Martins
    Replies:
    5
    Views:
    483
    Diego Martins
    Sep 5, 2006
  3. Ole Nielsby
    Replies:
    0
    Views:
    386
    Ole Nielsby
    May 8, 2007
  4. Frank Bergemann
    Replies:
    2
    Views:
    6,330
    Frank Bergemann
    Jun 27, 2009
  5. Luca Forlizzi
    Replies:
    6
    Views:
    459
    Shao Miller
    Aug 5, 2010
Loading...

Share This Page