trying to learn: const

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    trying to learn: const

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


  • Gianni Mariani

    #2
    Re: trying to learn: const

    <- Chameleon -> wrote:[color=blue]
    > what is the meaning of 2 *const* in following line?
    > -----------
    > const MyClass MyFunction const (MyClass &a);
    > -----------[/color]

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

    Comment

    • Martin Magnusson

      #3
      Re: trying to learn: const

      <- Chameleon -> wrote:
      [color=blue]
      > what is the meaning of 2 *const* in following line?
      > -----------
      > const MyClass MyFunction const (MyClass &a);
      > -----------[/color]

      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


      / martin


      Comment

      • Ron Natalie

        #4
        Re: trying to learn: const


        "Martin Magnusson" <loveslave@frus tratedhousewive s.zzn.com> wrote in message news:bkt109$qe5 $1@green.tninet .se...[color=blue]
        > <- Chameleon -> wrote:
        >[color=green]
        > > what is the meaning of 2 *const* in following line?
        > > -----------
        > > const MyClass MyFunction const (MyClass &a);
        > > -----------[/color]
        >
        > Is that legal code? If it were
        >[/color]
        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.



        Comment

        Working...