constness and inheritance

Discussion in 'C++' started by sndive@gmail.com, Jul 26, 2007.

  1. Guest

    struct A {
    virtual int foo(const int bar);
    };

    int A::foo(const int bar)
    {
    return 0;
    }
    struct B : public A {
    int foo(int bar);
    };
    int B::foo(int bar)
    {
    return 1;
    }

    int
    main()
    {
    A *p = new B;
    const int baz=-1;
    int r = p->foo(baz);
    return 0;
    }

    here B::foo is called (tried with g++3.2.3)
    i wonder if it's a problem for non elementary types in parameters
    since derived function can modify the baz argument with impunity
    contrary to the expectations of the caller.
    Granted, B::foo could've taken bar as the const parameter
    and casted to non const but there are no casts of any
    kind in the program above.
     
    , Jul 26, 2007
    #1
    1. Advertising

  2. wrote:
    > struct A {
    > virtual int foo(const int bar);
    > };
    >
    > int A::foo(const int bar)
    > {
    > return 0;
    > }
    > struct B : public A {
    > int foo(int bar);


    This 'foo' *overrides* 'A::foo' because it has exactly same type.

    > };
    > int B::foo(int bar)
    > {
    > return 1;
    > }
    >
    > int
    > main()
    > {
    > A *p = new B;
    > const int baz=-1;
    > int r = p->foo(baz);
    > return 0;
    > }
    >
    > here B::foo is called (tried with g++3.2.3)


    As it bloody well should.

    > i wonder if it's a problem for non elementary types in parameters
    > since derived function can modify the baz argument with impunity
    > contrary to the expectations of the caller.


    Top-level const qualifiers are ignored in the function declaration
    as far as type matching is concerned. In your program 'B::foo'
    and 'A::foo' have *exactly same* type.

    > Granted, B::foo could've taken bar as the const parameter
    > and casted to non const but there are no casts of any
    > kind in the program above.


    The top-level 'const' doesn't matter.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 26, 2007
    #2
    1. Advertising

  3. James Kanze Guest

    On Jul 26, 8:05 pm, wrote:
    > struct A {
    > virtual int foo(const int bar);
    > };


    > int A::foo(const int bar)
    > {
    > return 0;}


    > struct B : public A {
    > int foo(int bar);};


    > int B::foo(int bar)
    > {
    > return 1;
    > }


    > int
    > main()
    > {
    > A *p = new B;
    > const int baz=-1;
    > int r = p->foo(baz);
    > return 0;
    > }


    > here B::foo is called (tried with g++3.2.3)


    Correct. Top level const in a function argument is ignored in
    function types.

    > i wonder if it's a problem for non elementary types in parameters
    > since derived function can modify the baz argument with impunity
    > contrary to the expectations of the caller.


    What expectations of the caller? All the caller can expect is
    never to see the parameter. It's call by copy, remember.

    --
    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, Jul 27, 2007
    #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:
    392
    tom_usenet
    Nov 17, 2003
  2. Marc
    Replies:
    6
    Views:
    693
    Denis Remezov
    Jul 6, 2004
  3. Replies:
    14
    Views:
    836
    Ian Collins
    Apr 4, 2006
  4. Paul Bibbings
    Replies:
    2
    Views:
    341
    Paul Bibbings
    Apr 22, 2010
  5. Marcel Müller

    reference counting and constness

    Marcel Müller, Dec 23, 2012, in forum: C++
    Replies:
    3
    Views:
    240
    James Kanze
    Dec 28, 2012
Loading...

Share This Page