【发布时间】:2012-03-26 19:19:02
【问题描述】:
我有一个基类:
class Foo {
public:
virtual ~Foo() {}
static void printFoos()
{
std::vector<Foo*>::iterator it;
for(it=fooList.begin();it!=fooList.end();++it)
{
std::cout<<"Class: "<<(*it)->getClassName()<<"\n";
}
}
virtual const char* getClassName()=0;
static std::vector<Foo*> fooList;
};
还有一些派生类,举个例子:
class Bar : public Foo {
public:
Bar();
private:
const char* getClassName()
{
return "Bar";
}
};
上面的代码给出了“对 Foo::getClassName() 的未定义引用”,我假设这是因为代码想要调用 Foo::getClassName(),但是我如何让它像正常调用虚拟调用一样调用函数? IE。如何让它从 Foo 内部调用 Bar::getClassName()?
编辑:忘记继承了
【问题讨论】:
-
调用代码在哪里?可以贴一下吗?
-
至少在您上面的代码中,Bar 也不是从 Foo 派生的。
-
如果你在 Base 类中声明 getClassName 为 public,那么它不应该在 Derived 类中也是 public 吗?
-
您的代码在经过适当修改后不会出现来自
g++ -ansi -pedantic -Werror -Wall的错误。你用的是什么编译器? -
不可重现。 ideone.com/h1uIl
标签: c++ function virtual abstract-class