Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inheritance - method resolution

Reply
Thread Tools

inheritance - method resolution

 
 
Christopher
Guest
Posts: n/a
 
      04-07-2011
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;
}
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      04-07-2011
On Apr 7, 10:12*am, Christopher <cp...@austin.rr.com> 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;
> }


 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      04-07-2011
On Apr 7, 12:29*pm, Leigh Johnston <le...@i42.co.uk> wrote:
> On 07/04/2011 18:26, red floyd wrote:
>
>
>
>
>
> > On Apr 7, 10:12 am, Christopher<cp...@austin.rr.com> *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.


 
Reply With Quote
 
Christopher
Guest
Posts: n/a
 
      04-07-2011
On Apr 7, 12:59*pm, Christopher <cp...@austin.rr.com> wrote:
> On Apr 7, 12:29*pm, Leigh Johnston <le...@i42.co.uk> wrote:
>
>
>
>
>
> > On 07/04/2011 18:26, red floyd wrote:

>
> > > On Apr 7, 10:12 am, Christopher<cp...@austin.rr.com> *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?
 
Reply With Quote
 
Christopher
Guest
Posts: n/a
 
      04-07-2011
On Apr 7, 1:02*pm, Leigh Johnston <le...@i42.co.uk> wrote:
> On 07/04/2011 18:59, Christopher wrote:
>
>
>
>
>
> > On Apr 7, 12:29 pm, Leigh Johnston<le...@i42.co.uk> *wrote:
> >> On 07/04/2011 18:26, red floyd wrote:

>
> >>> On Apr 7, 10:12 am, Christopher<cp...@austin.rr.com> * *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
{
}
};

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-07-2011
On Apr 7, 10:29*am, Leigh Johnston <le...@i42.co.uk> wrote:
> On 07/04/2011 18:26, red floyd wrote:
>
>
>
>
>
>
>
>
>
> > On Apr 7, 10:12 am, Christopher<cp...@austin.rr.com> *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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LCD TV resolution / DVD resolution ? slonkak DVD Video 0 11-13-2006 02:34 PM
How do you change the Modelsim Cursor Resolution (not simulation resolution) Andrew FPGA VHDL 0 09-26-2005 04:05 AM
Scanning resolution, printing resolution, and downsampling hassy_user Digital Photography 11 10-27-2004 07:18 PM
Resolution resolution Simon Digital Photography 4 02-27-2004 01:53 PM
ISO Resolution Chart and Printing Resolution Jack Yeazel Digital Photography 0 08-11-2003 11:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57