trying to learn: const

Discussion in 'C++' started by Guest, Sep 24, 2003.

  1. Guest

    Guest Guest

    what is the meaning of 2 *const* in following line?
    -----------
    const MyClass MyFunction const (MyClass &a);
    -----------
     
    Guest, Sep 24, 2003
    #1
    1. Advertising

  2. <- Chameleon -> wrote:
    > what is the meaning of 2 *const* in following line?
    > -----------
    > const MyClass MyFunction const (MyClass &a);
    > -----------


    I suspect you mean somthing like this because what you have above is not
    standard C++.

    struct MyClass
    {
    const MyClass MyFunction (MyClass &a) const;
    };


    The first const is saying that the value returned is a const - i.e. it
    may not be changed. The second const indicates the constness of "this".
     
    Gianni Mariani, Sep 24, 2003
    #2
    1. Advertising

  3. <- Chameleon -> wrote:

    > what is the meaning of 2 *const* in following line?
    > -----------
    > const MyClass MyFunction const (MyClass &a);
    > -----------


    Is that legal code? If it were

    const MyClass* MyFunction( MyClass &a ) const;

    the last const would mean that MyFunction can't change any of the fields
    (member variables) of MyClass, and the first const would mean that
    MyFunction returns a constant pointer to a MyClass object -- that is,
    the returned pointer cannot be used to change what it points to.

    For more info on const, look at
    http://www.possibility.com/Cpp/const.html and
    http://www.parashift.com/c -faq-lite/const-correctness.html

    / martin
     
    Martin Magnusson, Sep 24, 2003
    #3
  4. Guest

    Ron Natalie Guest

    "Martin Magnusson" <> wrote in message news:bkt109$qe5$...
    > <- Chameleon -> wrote:
    >
    > > what is the meaning of 2 *const* in following line?
    > > -----------
    > > const MyClass MyFunction const (MyClass &a);
    > > -----------

    >
    > Is that legal code? If it were
    >

    No. The last const after the function name is not allowed.
    The first const is OK, but sort of spurious since the return
    value usually gets copied anyhow, it will lose the constness.
     
    Ron Natalie, Sep 24, 2003
    #4
    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:
    11
    Views:
    1,109
  2. Javier
    Replies:
    2
    Views:
    571
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,124
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    542
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page