inheritance - method resolution

Discussion in 'C++' started by Christopher, Apr 7, 2011.

  1. Christopher

    Christopher Guest

    Where is the rule that explains why this will not compile? I've always
    expected this to work, but it would appear that I haven't run into
    this problem yet.

    To resolve the problem, do I really need to override every single
    method from the Base with the same name as the specific method I am
    interested in overriding? I have a good 20 of them in production code.

    A simple test case to reproduce what I am experiencing in more
    complicated code:


    class Base
    {
    public:
    virtual void Foo()
    {
    }

    void Foo(int x)
    {

    }
    };

    class Derived : public Base
    {
    public:
    void Foo()
    {
    }
    };

    int main()
    {

    Derived * blah = new Derived();
    blah->Foo(5);
    delete blah;

    return 0;
    }
     
    Christopher, Apr 7, 2011
    #1
    1. Advertising

  2. Christopher

    red floyd Guest

    On Apr 7, 10:12 am, Christopher <> wrote:
    > Where is the rule that explains why this will not compile? I've always
    > expected this to work, but it would appear that I haven't run into
    > this problem yet.
    >
    > To resolve the problem, do I really need to override every single
    > method from the Base with the same name as the specific method I am
    > interested in overriding? I have a good 20 of them in production code.


    No. Use "using" (see below).
    >
    > class Base
    > {
    > public:
    >    virtual void Foo()
    >    {
    >    }
    >
    >    void Foo(int x)
    >    {
    >
    >    }
    >
    > };
    >
    > class Derived : public Base
    > {
    > public:
    >    void Foo()
    >    {
    >    }


    using Base::Foo(int);
    >
    > };
    >
    > int main()
    > {
    >
    >    Derived * blah = new Derived();
    >    blah->Foo(5);
    >    delete blah;
    >
    >    return 0;
    > }
     
    red floyd, Apr 7, 2011
    #2
    1. Advertising

  3. Christopher

    Christopher Guest

    On Apr 7, 12:29 pm, Leigh Johnston <> wrote:
    > On 07/04/2011 18:26, red floyd wrote:
    >
    >
    >
    >
    >
    > > On Apr 7, 10:12 am, Christopher<>  wrote:
    > >> Where is the rule that explains why this will not compile? I've always
    > >> expected this to work, but it would appear that I haven't run into
    > >> this problem yet.

    >
    > >> To resolve the problem, do I really need to override every single
    > >> method from the Base with the same name as the specific method I am
    > >> interested in overriding? I have a good 20 of them in production code.

    >
    > > No.  Use "using" (see below).

    >
    > >> class Base
    > >> {
    > >> public:
    > >>     virtual void Foo()
    > >>     {
    > >>     }

    >
    > >>     void Foo(int x)
    > >>     {

    >
    > >>     }

    >
    > >> };

    >
    > >> class Derived : public Base
    > >> {
    > >> public:
    > >>     void Foo()
    > >>     {
    > >>     }

    >
    > >       using Base::Foo(int);

    >
    > That is ill-formed code; instead use:
    >
    >         using Base::Foo;
    >
    > /Leigh- Hide quoted text -
    >
    > - Show quoted text -



    I don't really have the option of changing the anything where it is
    called. That would be thousands of places in code that had already
    been written before I cam aboard and I am sure the bosses would frown
    on altering working stuff.

    I am thinking of renaming the method I want to override to FooHelper.

    I do wonder what the rule is though?..., so I can quote it in my
    comments.
     
    Christopher, Apr 7, 2011
    #3
  4. Christopher

    Christopher Guest

    On Apr 7, 12:59 pm, Christopher <> wrote:
    > On Apr 7, 12:29 pm, Leigh Johnston <> wrote:
    >
    >
    >
    >
    >
    > > On 07/04/2011 18:26, red floyd wrote:

    >
    > > > On Apr 7, 10:12 am, Christopher<>  wrote:
    > > >> Where is the rule that explains why this will not compile? I've always
    > > >> expected this to work, but it would appear that I haven't run into
    > > >> this problem yet.

    >
    > > >> To resolve the problem, do I really need to override every single
    > > >> method from the Base with the same name as the specific method I am
    > > >> interested in overriding? I have a good 20 of them in production code.

    >
    > > > No.  Use "using" (see below).

    >
    > > >> class Base
    > > >> {
    > > >> public:
    > > >>     virtual void Foo()
    > > >>     {
    > > >>     }

    >
    > > >>     void Foo(int x)
    > > >>     {

    >
    > > >>     }

    >
    > > >> };

    >
    > > >> class Derived : public Base
    > > >> {
    > > >> public:
    > > >>     void Foo()
    > > >>     {
    > > >>     }

    >
    > > >       using Base::Foo(int);

    >
    > > That is ill-formed code; instead use:

    >
    > >         using Base::Foo;

    >
    > > /Leigh- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > I don't really have the option of changing the anything where it is
    > called. That would be thousands of places in code that had already
    > been written before I cam aboard and I am sure the bosses would frown
    > on altering working stuff.
    >
    > I am thinking of renaming the method I want to override to FooHelper.
    >
    > I do wonder what the rule is though?..., so I can quote it in my
    > comments.- Hide quoted text -
    >
    > - Show quoted text -


    Hmm, I might have misunderstood. Where are you putting the using
    statement?
     
    Christopher, Apr 7, 2011
    #4
  5. Christopher

    Christopher Guest

    On Apr 7, 1:02 pm, Leigh Johnston <> wrote:
    > On 07/04/2011 18:59, Christopher wrote:
    >
    >
    >
    >
    >
    > > On Apr 7, 12:29 pm, Leigh Johnston<>  wrote:
    > >> On 07/04/2011 18:26, red floyd wrote:

    >
    > >>> On Apr 7, 10:12 am, Christopher<>    wrote:
    > >>>> Where is the rule that explains why this will not compile? I've always
    > >>>> expected this to work, but it would appear that I haven't run into
    > >>>> this problem yet.

    >
    > >>>> To resolve the problem, do I really need to override every single
    > >>>> method from the Base with the same name as the specific method I am
    > >>>> interested in overriding? I have a good 20 of them in production code.

    >
    > >>> No.  Use "using" (see below).

    >
    > >>>> class Base
    > >>>> {
    > >>>> public:
    > >>>>      virtual void Foo()
    > >>>>      {
    > >>>>      }

    >
    > >>>>      void Foo(int x)
    > >>>>      {

    >
    > >>>>      }

    >
    > >>>> };

    >
    > >>>> class Derived : public Base
    > >>>> {
    > >>>> public:
    > >>>>      void Foo()
    > >>>>      {
    > >>>>      }

    >
    > >>>        using Base::Foo(int);

    >
    > >> That is ill-formed code; instead use:

    >
    > >>          using Base::Foo;

    >
    > >> /Leigh- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > I don't really have the option of changing the anything where it is
    > > called. That would be thousands of places in code that had already
    > > been written before I cam aboard and I am sure the bosses would frown
    > > on altering working stuff.

    >
    > > I am thinking of renaming the method I want to override to FooHelper.

    >
    > > I do wonder what the rule is though?..., so I can quote it in my
    > > comments.

    >
    > You put the using declaration inside the derived class not where it is
    > called; read replies more carefully.
    >
    > /Leigh- Hide quoted text -
    >
    > - Show quoted text -




    Ok This works, but can you explain what that using directive is doing?
    If I read the docs on the using directive, I'd expect these to be
    equivalent:

    class Derived : public Base
    {
    public:

    using Base::Foo;
    void Foo()
    {
    }
    };

    class Derived : public Base
    {
    public:

    void Base::Foo() // This makes no sense and the compiler tells me
    so
    {
    }
    };
     
    Christopher, Apr 7, 2011
    #5
  6. Christopher

    red floyd Guest

    On Apr 7, 10:29 am, Leigh Johnston <> wrote:
    > On 07/04/2011 18:26, red floyd wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On Apr 7, 10:12 am, Christopher<>  wrote:
    > >> Where is the rule that explains why this will not compile? I've always
    > >> expected this to work, but it would appear that I haven't run into
    > >> this problem yet.

    >
    > >> To resolve the problem, do I really need to override every single
    > >> method from the Base with the same name as the specific method I am
    > >> interested in overriding? I have a good 20 of them in production code.

    >
    > > No.  Use "using" (see below).

    >
    > >> class Base
    > >> {
    > >> public:
    > >>     virtual void Foo()
    > >>     {
    > >>     }

    >
    > >>     void Foo(int x)
    > >>     {

    >
    > >>     }

    >
    > >> };

    >
    > >> class Derived : public Base
    > >> {
    > >> public:
    > >>     void Foo()
    > >>     {
    > >>     }

    >
    > >       using Base::Foo(int);

    >
    > That is ill-formed code; instead use:
    >
    >         using Base::Foo;


    Oops. Brain fart. Thanks.
     
    red floyd, Apr 7, 2011
    #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. Andrew FPGA
    Replies:
    0
    Views:
    1,010
    Andrew FPGA
    Sep 26, 2005
  2. Frank

    dynamic method resolution

    Frank, Apr 23, 2004, in forum: Java
    Replies:
    1
    Views:
    557
    chris
    Apr 23, 2004
  3. Replies:
    8
    Views:
    7,015
    Michele Simionato
    Jun 27, 2006
  4. ddtl
    Replies:
    4
    Views:
    391
    Bruno Desthuilliers
    Sep 10, 2006
  5. Srini
    Replies:
    3
    Views:
    362
    Srini
    Aug 16, 2005
Loading...

Share This Page