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. Advertisements

  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. Advertisements

  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. Advertisements

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. Porky Pig Jr
    Replies:
    3
    Views:
    1,450
    Fuzzyman
    May 12, 2004
  2. CoreyWhite
    Replies:
    1
    Views:
    820
    JohnQ
    Mar 31, 2007
  3. Replies:
    11
    Views:
    1,406
  4. Javier
    Replies:
    2
    Views:
    895
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,483
    Tim Rentsch
    Nov 10, 2008
  6. fungus
    Replies:
    13
    Views:
    1,226
    fungus
    Oct 31, 2008
  7. Replies:
    2
    Views:
    692
    Andrew Koenig
    Feb 9, 2009
  8. Alexander
    Replies:
    20
    Views:
    1,380
    BGB / cr88192
    Sep 11, 2010
Loading...