Re: Compiler Warning C4373 about virtual methods
Thanks Igor,
I have verified you are correct. Here is my verification code and related
compile error message.
The point you mentioned in your below reply makes me confused about my
previous understanding about virtual function. In my previous understanding,
it does not matter the type of pointer, what matters is the type of class.
But in your code, you showed the type of pointer matters... Confused. It is
appreciated if you could share your point of how to decide which function is
called in the world of virtual. :-)
[Code]
#include <iostream>
using namespace std;
class Base {
public:
virtual void foo() {cout << "Base " << endl; }
};
class Derived : public Base {
public:
virtual void foo (int i) {cout << "Derived " << i << endl; }
};
/*
output:
Base
Derived 100
*/
int main()
{
Derived d;
Base* pb = &d;
Derived* pd = &d;
pb -> foo();
// pb->foo (100); // error C2660: 'Base::foo' : function does not take 1
arguments
pd -> foo(100);
// pd->foo(); // error C2660: 'Derived::foo' : function does not take 0
arguments
return 0;
}
[/Code]
regards,
George
"Igor Tandetnik" wrote:
"George" <George@discussions.microsoft.com> wrote in message
news:B9264F92-15DE-4386-BAED-7B4FB19667DF@microsoft.com
Your first statement is the opposite of what Igor said.
why my statement is opposite?
I said:
<quote>
void f(int n);
void f(const int n);
both declare the exact same function.
</quote>
You paraphrased it as "the methods differ in const are treated as
different methods".
"Exact same" is the opposite of "different". Look it up in your favorite
English thesaurus.
Through my testing and MSDN comments,
the derived method is called in Visual Studio 2008
Which proves it's treated as an override of the method in the base
class, which it would if their signatures were considered matching. If
the method in the derived class would be treated as having a different
signature from that of the base, it wouldn't override the base class
function, and your call through the base class pointer would end up
invoking the base class member.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925