我们有以下两个代码:
首先:
class base
{
public:
static void nothing()
{
std::cout << "Nothing!" << std::endl;
}
};
class derived : public base
{
public:
static void nothing(int a)
{
std::cout << "Nothing with " << a << std::endl;
}
static void nothing(double b)
{
std::cout << "Nothing with " << b << std::endl;
}
//using base::nothing;
};
int main()
{
derived::nothing();
return 0;
}
和第二个:
class base
{
public:
static void nothing()
{
std::cout << "Nothing!" << std::endl;
}
};
class derived : public base
{
public:
/*static void nothing(int a)
{
std::cout << "Nothing with " << a << std::endl;
}
static void nothing(double b)
{
std::cout << "Nothing with " << b << std::endl;
}
*/
//using base::nothing;
};
int main()
{
derived::nothing();
return 0;
}
问题,为什么我们无法编译第一个代码? 编译器无法从基类中找到继承的静态方法,但只能看到派生类中的方法!
要编译第一个例子,我们需要通过'using'声明引入base :: nothing名称。
有人可以告诉我为什么吗? 为什么在派生类中声明静态'nothing'方法会阻止编译器从基类中看到继承的静态方法?