我的基本理解是没有纯虚函数的实现,但是,有人告诉我可能有纯虚函数的实现.
class A {
public:
virtual void f() = 0;
};
void A::f() {
cout<<"Test"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
代码是否正常?
使其成为具有实现的纯虚函数的目的是什么?
在GCC上编译时,我得到错误:函数定义上的纯指定符,但是当我使用VS2005编译相同的代码时却没有.
class Dummy {
//error: pure-specifier on function-definition, VS2005 compiles
virtual void Process() = 0 {};
};
Run Code Online (Sandbox Code Playgroud)
但是当这个纯虚函数的定义不是内联时,它的工作原理是:
class Dummy
{
virtual void Process() = 0;
};
void Dummy::Process()
{} //compiles on both GCC and VS2005
Run Code Online (Sandbox Code Playgroud)
错误意味着什么?为什么我不能内联?如第二个代码示例所示,避免编译问题是否合法?
除了可以从派生类重写方法调用它之外,纯虚函数似乎无法在任何地方调用!那么,它的使用是什么?例如.
class base
{
public:
virtual void fun()=0
{
cout<<"I have been called from derived class"<<endl;
}
};
class derived:public base
{
public:
void fun()
{
cout<<"I am the derived class"<<endl;
base::fun();
}
};
void main()
{
derived d;
d.fun();
}
Run Code Online (Sandbox Code Playgroud)