我在C++中面臨的一個問題:C++重寫/過載問題
#include <iostream>
class A
{
protected:
void some_func(const unsigned int& param1)
{
std::cout << "A::some_func(" << param1 << ")" << std::endl;
}
public:
virtual ~A() {}
virtual void some_func(const unsigned int& param1, const char*)
{
some_func(param1);
}
};
class B : public A
{
public:
virtual ~B() {}
virtual void some_func(const unsigned int& param1, const char*)
{
some_func(param1);
}
};
int main(int, char**)
{
A* t = new B();
t->some_func(21, "some char*");
return 0;
}
我用G ++ 4.0.1編譯錯誤:
$ g++ -W -Wall -Werror test.cc
test.cc: In member function ‘virtual void B::some_func(const unsigned int&, const char*)’:
test.cc:24: error: no matching function for call to ‘B::some_func(const unsigned int&)’
test.cc:22: note: candidates are: virtual void B::some_func(const unsigned int&, const char*)
爲什麼我必須指定類B中some_func(param1)的調用是A :: some_func(param1)?它是一個g ++ bug還是來自g ++的隨機消息,以防止我看不到的特殊情況?
這很奇怪。受保護的基類函數應該對子類可見,虛擬,重載或不可重載。 – 2009-09-27 22:30:23