inheritance - method resolution

C

Christopher

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;
}
 
R

red floyd

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);
 
C

Christopher

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.
 
C

Christopher

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?
 
C

Christopher

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
{
}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top