however, above code gives me a compilation error saying
no matching function for call to ‘B::f(int)’
candidates are: virtual void B::f(int, int)
why do i have to use A:: in front of f(0) in g() to make this compile?
aren't f(int,int) and f(int) two different functions (because they have different parameter lists) and B's VFT should have a function pointer to f in A?
That is, any reference to f(int) in B should point to the correct function in A.
Whether you use classes or structs makes no difference. I tried making them classes as eker676 suggested and it made no difference whatsoever.
1 2 3 4 5 6 7 8 9 10
class A {
public:
virtualvoid f(int) { cout << "Class A" << endl; }
};
class B: public A {
public:
virtualvoid f(int,int) { cout << "Class B" << endl; }
void g() { f(0); }
};
The point of using the keyword public is important if you are dealing with classes since functions are private by default in classes. Also, inheritance is private by default with classes.
I also get this warning:
"src\hello.cc", line 19: warning #997-D: function "A::f(int)" is hidden by
"B::f" -- virtual function override intended?
virtual void f(int,int) { cout << "Class B" << endl; }
I thought that the hiding problem only occurred while redefining inherited non-virtual functions. I guess I was wrong! To be honest, I have never seen anyone try something like this. Obviously, the compiler doesn't see A's definition of f when compiling B because it is warning you that it is hidden! Sorry to say, I cannot give you a full explanation of why.