simple question on const function member passing

Discussion in 'C++' started by Giff, Apr 1, 2007.

  1. Giff

    Giff Guest

    Hi,

    I hope you can clear a doubt that is in my mind:

    the function:

    void food(const type* ptrToType);

    means that it should not modify the object pointed by the pointer, or
    merely the address of the pointer (and so the object might be
    changed)?

    Many thanks
    Giff, Apr 1, 2007
    #1
    1. Advertising

  2. Giff

    SasQ Guest

    Dnia Sun, 01 Apr 2007 03:15:00 -0700, Giff napisa³(a):

    > void food(const type* ptrToType);
    >
    > means that it should not modify the object pointed by the
    > pointer, or merely the address of the pointer (and so the
    > object might be changed)?


    If you're in doubt, check where the const is. If it's near
    the type, that means the object of that type pointed to is
    const. If it's near the name of the pointer itself, it
    means that pointer itself is constant and you can't point it
    to another object.

    --
    SasQ
    SasQ, Apr 1, 2007
    #2
    1. Advertising

  3. Giff

    Giff Guest

    On 1 Apr, 12:32, SasQ <> wrote:

    > If you're in doubt, check where the const is. If it's near
    > the type, that means the object of that type pointed to is
    > const. If it's near the name of the pointer itself, it
    > means that pointer itself is constant and you can't point it
    > to another object.



    thanks, then it must be the pointed object :)
    Giff, Apr 1, 2007
    #3
  4. Giff

    Jim Langston Guest

    "Giff" <> wrote in message
    news:...
    >
    > Hi,
    >
    > I hope you can clear a doubt that is in my mind:
    >
    > the function:
    >
    > void food(const type* ptrToType);
    >
    > means that it should not modify the object pointed by the pointer, or
    > merely the address of the pointer (and so the object might be
    > changed)?
    >
    > Many thanks


    This can also be written as:
    void food(type const* ptrToType);
    Now read it right to left.

    ptrToType is a pointer to a constant type.

    If it was written this way:
    void food(type* const ptrToType);
    read it right to left again.

    ptrToType is a constant pointer to type.

    Using your original syntax works but a little more ackwardly.

    void food(const type* ptrToType);

    ptrToType is a pointer to type which is constant.
    Jim Langston, Apr 1, 2007
    #4
  5. Giff

    Giff Guest

    On 1 Apr, 15:32, "Jim Langston" <> wrote:

    > This can also be written as:
    > void food(type const* ptrToType);
    > Now read it right to left.
    >
    > ptrToType is a pointer to a constant type.


    Thank you, I'll try to use this form from now on.
    Giff, Apr 1, 2007
    #5
  6. Giff

    Jim Langston Guest

    "Giff" <> wrote in message
    news:...
    > On 1 Apr, 15:32, "Jim Langston" <> wrote:
    >
    >> This can also be written as:
    >> void food(type const* ptrToType);
    >> Now read it right to left.
    >>
    >> ptrToType is a pointer to a constant type.

    >
    > Thank you, I'll try to use this form from now on.


    Most code I've seen uses:
    const type* ptrToType

    There is no hard and fast rule. As long as you understand what it's saying,
    pick one way and stick with it. Neither way is wrong.
    Jim Langston, Apr 1, 2007
    #6
  7. Giff

    James Kanze Guest

    On Apr 1, 10:26 pm, "Jim Langston" <> wrote:
    > "Giff" <> wrote in message


    > news:...


    > > On 1 Apr, 15:32, "Jim Langston" <> wrote:


    > >> This can also be written as:
    > >> void food(type const* ptrToType);
    > >> Now read it right to left.


    > >> ptrToType is a pointer to a constant type.


    > > Thank you, I'll try to use this form from now on.


    > Most code I've seen uses:
    > const type* ptrToType


    I've seen both.

    > There is no hard and fast rule. As long as you understand
    > what it's saying, pick one way and stick with it. Neither way
    > is wrong.


    That's true up to a point. All other things being equal, the
    post position is more coherent. All other things are never
    equal however. Also: if your company or project has chosen one,
    choosing the other *is* wrong.

    --
    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, Apr 2, 2007
    #7
    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. Newsgroup - Ann
    Replies:
    5
    Views:
    592
    John Carson
    Jul 30, 2003
  2. Replies:
    8
    Views:
    544
    PJP of NYC
    May 24, 2005
  3. Azdo
    Replies:
    2
    Views:
    428
  4. Javier
    Replies:
    2
    Views:
    547
    James Kanze
    Sep 4, 2007
  5. fungus
    Replies:
    13
    Views:
    873
    fungus
    Oct 31, 2008
Loading...

Share This Page