a function follow by const

Discussion in 'C++' started by moongeegee, May 21, 2008.

  1. moongeegee

    moongeegee Guest

    what does it mean/purpose ?

    class xxx
    {

    }
    moongeegee, May 21, 2008
    #1
    1. Advertising

  2. moongeegee

    moongeegee Guest

    press the send by mistake.

    What does the "const" mean/purpose ?? Thanks.

    class xxx
    {
    double getNum() const;
    }
    moongeegee, May 21, 2008
    #2
    1. Advertising

  3. moongeegee

    moongeegee Guest

    Sent by mistake.

    What does the "const" mean/purpose ?? Thanks

    class xxx
    {
    double getNum() const;
    }
    moongeegee, May 21, 2008
    #3
  4. moongeegee a écrit :
    > Sent by mistake.
    >
    > What does the "const" mean/purpose ?? Thanks
    >
    > class xxx
    > {
    > double getNum() const;
    > }


    it means that, is you have a const instance of the class xxx, you will
    be able to call getNum on it:

    const xxx theXXX;
    double d = theXXX.getNum();

    When you declare a member function as const, the compiler check that the
    function does not modify the object it is called on.

    You can see those const functions as 'observers' of you class, or
    'getters' (hence the name 'getYYY')

    Cheers,
    --
    Vincent Jacques

    "S'il n'y a pas de solution, c'est qu'il n'y a pas de problème"
    Devise Shadock
    Vincent Jacques, May 21, 2008
    #4
  5. moongeegee

    Stefan Ram Guest

    Victor Bazarov <> writes:
    >Vincent Jacques wrote:
    >>it means that, is you have a const instance of the class xxx,
    >>you will be able to call getNum on it:

    >You seem to imply that for a non-const instance of 'xxx'
    >'getNum' cannot be called.


    A -> B does not imply ¬A -> ¬B.

    >That's not true.


    This comment applies to your reading,
    not to Vincents text.
    Stefan Ram, May 21, 2008
    #5
  6. moongeegee

    James Kanze Guest

    On May 21, 9:47 pm, Vincent Jacques <>
    wrote:
    > moongeegee a écrit :


    > > Sent by mistake.


    > > What does the "const" mean/purpose ?? Thanks


    > > class xxx
    > > {
    > > double getNum() const;
    > > }


    > it means that, is you have a const instance of the class xxx,
    > you will be able to call getNum on it:


    > const xxx theXXX;
    > double d = theXXX.getNum();


    > When you declare a member function as const, the compiler
    > check that the function does not modify the object it is
    > called on.


    Formally, the const has two effects: you cannot call a non-const
    function on an object designated by an expression with const
    type (e.g. a const object, or a reference to const), and the
    type of this in the function will be ClassName const*, rather
    than simply ClassName* (i.e. you cannot modify the object in a
    const function, with a few special exceptions).

    Formally, too, the compiler applies what has usually been called
    "bit-wise const": if your class is something like:

    class C
    {
    char* p ;
    public:
    void f() const ;
    } ;

    C::f() can modify what p points to. Generally accepted
    practice, however, is to implement logical const: if what p
    points to is part of the "value" of your object, you don't
    modify it in a const function, even if the compiler allows it.
    (Similarly, if some direct member of your object is not part of
    its conceptual value, you may modify it in a const function,
    either by declaring it mutable or casting away const. This case
    occurs far less frequently, however.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 22, 2008
    #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:
    18
    Views:
    519
  2. avier
    Replies:
    7
    Views:
    301
    avier
    Aug 4, 2005
  3. Replies:
    11
    Views:
    1,098
  4. Javier
    Replies:
    2
    Views:
    556
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,108
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page