|
-
September 30th, 2010, 08:16 AM
#1
Calling functions in Pure virtual class
Hi, Folks,
I have pure virtual class questions:
class B
{
public:
virtual double foo(int a) = 0;
virtual double foo(int a, int b) =0;
}
class D : public B
{
public:
virtual double foo(int a)
{
doube c = foo(a, 1.0);
return c;
}
}
Compiling this gives error:
C2661: D:foo no overloaded function takes 2 arguments.
Does anyone understand what is going on?
Thanks for your help!
CR
-
September 30th, 2010, 08:24 AM
#2
Re: Calling functions in Pure virtual class
The compiler stops searching the base class for a method called foo() when it sees one in the derived class, unless you explicitly tell it to do so:
Code:
class D : public B
{
public:
using B::foo;
virtual double foo(int a)
{
doube c = foo(a, 1.0);
return c;
}
}
Note, in this limited example this still won't compile, since it will say that D is an abstract class since nothing in the hierarchy provides a concrete implementation of foo(int,int) which is pure virtual.
-
September 30th, 2010, 08:47 AM
#3
Re: Calling functions in Pure virtual class
Hi, Lindley,
Thanks for your quick reply.
foo(a, b) is also a pure virtual function, which will be defined later in another class, say, E. If I explicilty tell D to call B::foo, will it invoke E:foo(a, b) in the end, or complain that B:foo(a,b) is pure virtual?
As I understand, what "using B::foo" really does is to embed B:foo in the scope of D. If so, will it be equivalent to repeating declaring foo(int a,int b) in D even if it is pure virtual?
Thanks,
CR
 Originally Posted by Lindley
The compiler stops searching the base class for a method called foo() when it sees one in the derived class, unless you explicitly tell it to do so:
Code:
class D : public B
{
public:
using B::foo;
virtual double foo(int a)
{
doube c = foo(a, 1.0);
return c;
}
}
Note, in this limited example this still won't compile, since it will say that D is an abstract class since nothing in the hierarchy provides a concrete implementation of foo(int,int) which is pure virtual.
-
September 30th, 2010, 09:02 AM
#4
Re: Calling functions in Pure virtual class
That should work fine. The trouble would only arise if you tried to instantiate a D.
-
September 30th, 2010, 12:38 PM
#5
Re: Calling functions in Pure virtual class
Yes, it works. However, i still don't understand why the compiler stops searching the base class for a method called foo() when it sees one in the derived class. It is not intuitive to me, although perhaps there is a good reason for it.
Thank you very much!
-
September 30th, 2010, 12:40 PM
#6
Re: Calling functions in Pure virtual class
The following code also compiles (at least on my system):
Code:
class B
{
public:
virtual double foo(int a) = 0;
virtual double foo(int a, int b) =0;
};
class D : public B
{
public:
virtual double foo(int a)
{
double c = ((B*)this)->foo(a, 1.0);
return c;
}
};
It would call the second choice of foo virtually (what fails to compile if you actually would try to instantiate a D)
IMO, the compiler error isn't because of the pure virtual function not yet implemented, but because functions with same name but different arguments cannot be resolved over class hierarchies. If the class D has a foo function the compiler stops to look for baseclass. So, the compiler would never consider B:foo(int, int) beside you would tell him explicitly using the class scope or cast your instance to a B.
-
September 30th, 2010, 01:00 PM
#7
Re: Calling functions in Pure virtual class
 Originally Posted by caperover2002
However, i still don't understand why the compiler stops searching the base class for a method called foo() when it sees one in the derived class. It is not intuitive to me, although perhaps there is a good reason for it.
Yeah, it is not intuitive. This is part of what Meyers wrote in Item 33 of Effective C++, 3rd edition:
 Originally Posted by Scott Meyers
The rationale behind this behavior is that it prevents you from accidentally inheriting overloads from distant base classes when you create a new derived class in a library or application framework. Unfortunately, you typically want to inherit the overloads. In fact, if you're using public inheritance and you don't inherit the overloads, you're violating the is-a relationship between base and derived classes that Item 32 explains is fundamental to public inheritance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|