相关疑难解决方法(0)

纯虚函数与实现

我的基本理解是没有纯虚函数的实现,但是,有人告诉我可能有纯虚函数的实现.

class A {
public:
    virtual void f() = 0;
};

void A::f() {
    cout<<"Test"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

代码是否正常?

使其成为具有实现的纯虚函数的目的是什么?

c++ pure-virtual

162
推荐指数
6
解决办法
8万
查看次数

函数定义的纯粹说明符

在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)

错误意味着什么?为什么我不能内联?如第二个代码示例所示,避免编译问题是否合法?

c++ abstract-class pure-virtual

34
推荐指数
3
解决办法
1万
查看次数

在c ++中使用纯虚函数的主体有什么用?

除了可以从派生类重写方法调用它之外,纯虚函数似乎无法在任何地方调用!那么,它的使用是什么?例如.

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)

c++

0
推荐指数
1
解决办法
293
查看次数

标签 统计

c++ ×3

pure-virtual ×2

abstract-class ×1