I would have thought that the call will now be passed to the base
class's implementation of AFunction, taking a bool parameter, which the
derived class inherits from the base class. Instead in VC++ 8.0, the
call goes to the derived class's function which takes an int parameter.
Is VC++ 8.0 wrong or, if not, what I am I missing in C++ that explains
what is happening ?
Here is the code in a VC++ 8.0 console application:
#include "stdafx.h"
#include "iostream"
namespace ClassHierarchyCppNative {
class BaseClassCppNative
{
protected:
virtual void AFunction(bool);
};
class DerivedClassCppNative : public BaseClassCppNative
{
public:
void AFunction(int);
};
class TestClassCppNative : public DerivedClassCppNative
{
public:
void CallAFunction(bool);
protected:
void AFunction(bool);
};
}
void ClassHierarchyCppNative::BaseClassCppNative::AFunction(bool p)
{
std::cout << "BaseClassCppNative: AFunction(bool) - bool = " << p <<
".\n";
}
void ClassHierarchyCppNative::DerivedClassCppNative::AFunction(int i)
{
std::cout << "DerivedClassCppNative: AFunction(int) - int = " << i <<
".\n";
}
void ClassHierarchyCppNative::TestClassCppNative::CallAFunction(bool b)
{
std::cout << "TestClassCppNative: CallAFunction(bool):begin - bool =
" << b << ".\n";
AFunction(b);
std::cout << "TestClassCppNative: CallAFunction(bool):end - bool = "
<< b << ".\n";
}
void ClassHierarchyCppNative::TestClassCppNative::AFunction(bool b)
{
std::cout << "TestClassCppNative: AFunction(bool):begin - bool = " <<
b << ".\n";
ClassHierarchyCppNative::DerivedClassCppNative::AFunction(b);
std::cout << "TestClassCppNative: AFunction(bool):end - bool = " << b
<< ".\n";
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "ConsoleTestCppNative:begin\n";
std::cout << "ConsoleTestCppNative instantiating TestClassCppNative\n";
ClassHierarchyCppNative::TestClassCppNative * tcc(new
ClassHierarchyCppNative::TestClassCppNative);
std::cout << "ConsoleTestCppNative calling
TestClassCppNative::CallAFunction passing true.\n";
tcc -> CallAFunction(true);
delete tcc;
std::cout << "ConsoleTestCppNative:end\n";
return 0;
}
Console output:
ConsoleTestCppNative:begin
ConsoleTestCppNative instantiating TestClassCppNative
BaseClassCppNative: constructor.
DerivedClassCppNative: constructor.
TestClassCppNative: constructor.
ConsoleTestCppNative calling TestClassCppNative::CallAFunction passing true.
TestClassCppNative: CallAFunction(bool):begin - bool = 1.
TestClassCppNative: AFunction(bool):begin - bool = 1.
DerivedClassCppNative: AFunction(int) - int = 1.
TestClassCppNative: AFunction(bool):end - bool = 1.
TestClassCppNative: CallAFunction(bool):end - bool = 1.
ConsoleTestCppNative:end
You can see in the line:
"DerivedClassCppNative: AFunction(int) - int = 1."
what is actually being called as the call goes down the chain. According
to VC++ the base class's AFunction(bool) is not among the overloaded set
of functions considered when I call down the chain. If this is correct,
why is it so ?
Thanks !
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
> Is VC++ 8.0 wrong or, if not, what I am I missing in C++ that
> explains what is happening ?
VC++ is right.
> #include "iostream"
Not related to your question, but that should be
#include <iostream>
> void ClassHierarchyCppNative::TestClassCppNative::AFunction(bool b)
> {
> std::cout << "TestClassCppNative: AFunction(bool):begin - bool = " <<
> b << ".\n";
> ClassHierarchyCppNative::DerivedClassCppNative::AFunction(b);
This tells the compiler to search for a funtion AFunction, starting in
the scope of class
ClassHierarchyCppNative::DerivedClassCppNative. There is exactly one
function of that name in that scope, which is a viable function, so it
is called.
> You can see in the line:
>
> "DerivedClassCppNative: AFunction(int) - int = 1."
>
> what is actually being called as the call goes down the chain. According
> to VC++ the base class's AFunction(bool) is not among the overloaded set
> of functions considered when I call down the chain. If this is correct,
> why is it so ?
Because overloading is restricted to one scope. In your example, that
scope is class DerivedClassCppNative.
Thanks ! I have certainly fallen asleep over this issue which I should
have known but, because I have rarely encountered, did not realize its
ramifications. Evidently using the the same name of a function ( or data
member ) in a derived class which is in a base class hides that name in
the scope of the derived class. So for DerivedClassCppNative the
AFunction(bool) does not exist as an overload candidate function.
Thanks for verifying my error. Even after all these years of programming
C++ I am still learning and realizing some things which I either did not
know, or in which my understanding had remained hazy.