C ++抽象基类构造函数/析构函数 - 一般正确性 [英] C++ abstract base class constructors/destructors - general correctness

查看:161
本文介绍了C ++抽象基类构造函数/析构函数 - 一般正确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我作为一名开发人员愚蠢,所以我冒险尝试了一本C ++书籍并学习如何正确地做事。在我的脑海中,我知道自己想做什么。我实际上想要一个接口,当继承时,必须重写(如果可能的话?)。到目前为止,我有以下内容:

Recently I have dumb as a developer, so I took the plunge, got a C++ book and learning how to do things properly. In my head, I know what I would like to do. I effectively want an Interface that when inherited, must be overridden (if this is possible?). So far, I have the following:

class ICommand{

public:
    //  Virtual constructor. Needs to take a name as parameter
    //virtual ICommand(char*) =0;
    //  Virtual destructor, prevents memory leaks by forcing clean up on derived classes?
    //virtual ~ICommand() =0; 
    virtual void CallMe() =0;
    virtual void CallMe2() =0;
};

class MyCommand : public ICommand
{
public:
    // Is this correct?
    MyCommand(char* Name) { /* do stuff */ }
    virtual void CallMe() {}
    virtual void CallMe2() {}
};

我故意离开了我认为构造函数/析构函数应该在中实现的方式的ICommand 。我知道如果删除注释,它将无法编译。请某人可以:

I have purposely left how I think the constructor/destructor's should be implemented in ICommand. I know if I remove the comments, it will not compile. Please could someone:


  1. 告诉我如何在 ICommand 中声明构造函数/析构函数以及它们如何用于 MyCommand

  2. 我是否在 ICommand中正确设置以便 MyCommand 必须覆盖 CallMe CallMe2

  1. Show me how to declare the constructor/destructor's in ICommand and how they are meant to be used in MyCommand
  2. Have I set things up correctly in ICommand so that MyCommand must override CallMe and CallMe2.

我希望我没有错过任何非常简单的事情......

推荐答案

C ++不允许使用虚拟构造函数。一个简单的实现(没有虚构造函数)看起来像这样:

C++ does not allow for virtual constructors. A simple implementation (without the virtual constructor) would look something like this:

class ICommand {
public:
    virtual ~ICommand() = 0;
    virtual void callMe() = 0;
    virtual void callMe2() = 0;
};

ICommand::~ICommand() { } // all destructors must exist



<请注意,即使是纯虚拟析构函数,也必须定义

Note that even a pure virtual destructor must be defined.

具体实现与您的示例完全相同:

A concrete implementation would look exactly like your example:

class MyCommand : public ICommand {
public:
    virtual void callMe() { }
    virtual void callMe2() { }
};

你有几个选项,用于构造函数。一个选项是禁用 ICommand 的默认构造函数,以便子类拥有来实现调用ICommand构造函数的构造函数:

You have a couple of options for the constructor. One option is to disable the default constructor for ICommand, so that subclasses will have to implement a constructor that calls your ICommand constructor:

#include <string>

class ICommand {
private:
    const std::string name;
    ICommand();
public:
    ICommand(const std::string& name) : name(name) { }
    virtual ~ICommand() = 0;
    virtual void callMe() = 0;
    virtual void callMe2() = 0;
};

ICommand::~ICommand() { } // all destructors must exist

具体实现现在看起来像这样:

A concrete implementation would now look something like this:

class MyCommand : public ICommand {
public:
    MyCommand(const std::string& name) : ICommand(name) { }
    virtual void callMe() { }
    virtual void callMe2() { }
};

这篇关于C ++抽象基类构造函数/析构函数 - 一般正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆