Constness as a parameter

Discussion in 'C++' started by MaxMax, Oct 19, 2005.

  1. MaxMax

    MaxMax Guest

    I have a problem of constness.

    struct MyS
    {
    int x;
    }

    void MyFunc(MyS* pA[])
    {
    }

    This func will receive an array of pointers to MyS. And everything is fine
    :)

    Now I want to add some const. I want the elements of the array (the
    pointers) to be const (so not changeable) (so no pA[0] = NULL; and I want
    the elements pointed to be not changeable (so no pA[0]->x = 0)... But I
    wasn't able to put the right const :-(
     
    MaxMax, Oct 19, 2005
    #1
    1. Advertising

  2. MaxMax

    Rolf Magnus Guest

    MaxMax wrote:

    > I have a problem of constness.
    >
    > struct MyS
    > {
    > int x;
    > }
    >
    > void MyFunc(MyS* pA[])
    > {
    > }
    >
    > This func will receive an array of pointers to MyS. And everything is fine
    > :)


    Actually, it takes a pointer to pointer to MyS.

    > Now I want to add some const. I want the elements of the array (the
    > pointers) to be const (so not changeable) (so no pA[0] = NULL; and I want
    > the elements pointed to be not changeable (so no pA[0]->x = 0)... But I
    > wasn't able to put the right const :-(


    Just put 'const' directly after the parts that you want to be constant:

    void MyFunc(MyS const * const pA[])
     
    Rolf Magnus, Oct 19, 2005
    #2
    1. Advertising

  3. MaxMax

    MaxMax Guest

    > Just put 'const' directly after the parts that you want to be constant:
    Yeah... I discovered it AFTER posting the message... I even tryed to cancel
    the message :-(
    But half of the problem was that C++ doesn't make the implicit conversions
    (so I had to typecast)

    --- bye
     
    MaxMax, Oct 19, 2005
    #3
    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. Martin Magnusson

    Casting away constness

    Martin Magnusson, Nov 17, 2003, in forum: C++
    Replies:
    1
    Views:
    391
    tom_usenet
    Nov 17, 2003
  2. Richard Hayden
    Replies:
    1
    Views:
    748
    Rob Williscroft
    Nov 23, 2003
  3. Trevor Lango

    Casting Away Constness

    Trevor Lango, Jan 2, 2004, in forum: C++
    Replies:
    15
    Views:
    2,412
    Ron Natalie
    Jan 2, 2004
  4. Marc
    Replies:
    6
    Views:
    693
    Denis Remezov
    Jul 6, 2004
  5. Replies:
    14
    Views:
    836
    Ian Collins
    Apr 4, 2006
Loading...

Share This Page