简体   繁体   English

c ++从病毒基本类继承

[英]c++ inherit from a virtal base class

I want to do the following: 我要执行以下操作:

class ErrorBase 
{
  public:
    void SetError(unsigned errorCode)
    {
      mErrorCode = errorCode;
    }

    char const* Explanation(unsigned errorCode) const
    {
      return errorExplanations[errorCode];
    }

  private:
    char const* errorExplanations[];
    unsigned mErrorCode;

};

class MyError : virtual public ErrorBase
{
  public:
    enum ErrorCodes {
      eNone,
      eGeneric,
      eMySpecificError
    };

    MyError() 
    { 

      // I want this to refer to the parent class's attribute, 
      // such that when Explanation() is executed, it uses this
      errorExplanations = {
        "no error",
        "error in MyClass",
        "specific error"
      }
    }
    ~MyError() { }
};

But I get the following error on the line declaring errorExplanations in the child class: 但是在子类中声明errorExplanations的行上出现以下错误:

error: expected primary-expression before '{' token 错误:“ {”令牌之前的预期主表达式

How do I declare errorExplanations in the child class such that I can instantiate a child, and call myChild.Explanation() and get one of the error strings defined in the child's constructor? 我如何在子类中声明errorExplanations以便实例化一个子类,然后调用myChild.Explanation()并获取在该子类的构造函数中定义的错误字符串之一?

Any suggestions/corrections regarding my usage of const , virtual , public , etc are appreciated, Thanks! 关于我使用constvirtualpublic等的任何建议/更正,谢谢!

Either you pass the array of error messages to the base class in its constructor (syntax may not be perfect but hopefully you get the idea): 您可以将错误消息数组传递给其构造函数中的基类(语法可能并不完美,但希望您能理解):

class ErrorBase {
  public:
    ErrorBase(char const* errorExplanations[]) {
      this->errorExplanations = errorExplanations;
    }
    ...
  private:
    char const* errorExplanations[];
    ...
};

class MyError : virtual public ErrorBase {
  public:
    ...
    MyError() :
      ErrorBase( {
        "no error",
        "error in MyClass",
        "specific error"
      } )
    { }
    ...
};

Or you make Explanation virtual and provide the desired implementation in the derived class: 或者您将Explanation虚拟化,并在派生类中提供所需的实现:

class ErrorBase {
  public:
    ...
    virtual char const* Explanation(unsigned errorCode) const = 0;
  protected:
    unsigned mErrorCode;
};

class MyError : virtual public ErrorBase {
  public:
    ...
    MyError() :
      errorExplanations( {
        "no error",
        "error in MyClass",
        "specific error"
      } )
    { }

    virtual char const* Explanation(unsigned errorCode) const {
      return errorExplanations[errorCode];
    }
    ...
  private:
    char const* errorExplanations[];
};

Well, one thing that's wrong is that you can't assign to arrays like that. 嗯,这是错误的一件事,就是您无法将其分配给这样的数组。 You can only initialize them that way. 您只能以这种方式初始化它们。 Since you've already initialized the array (to be empty) in the constructor's initialization section (which though empty is using default constructors), your array is initialized. 由于您已经在构造函数的初始化部分中初始化了该数组(为空)(尽管使用默认构造函数,该数组为空),所以对数组进行了初始化。

You'll need to assign to the array in one of the ways in which you would normally assign to arrays, such as memcpy or a for loop. 您需要以通常分配给数组的一种方式分配给数组,例如memcpy或for循环。

Another thing that's wrong is you don't actually have access to the array where you're trying to assign to it. 另一件事是错误的,您实际上没有访问您要分配给它的数组的权限。 You'll need to expose it to subclasses with protected or have an assignment function. 您需要将其公开给具有protected或具有赋值函数的子类。

Here's another option. 这是另一个选择。 Have the base class get the data array via a virtual function: 让基类通过虚函数获取数据数组:

class ErrorBase 
{
  public:
    void SetError(unsigned errorCode)
    {
      mErrorCode = errorCode;
    }

    char const* Explanation(unsigned errorCode) const
    {
      return GetErrorTable()[errorCode];
    }

  private:
    virtual char const **GetErrorTable() const = 0;

  private:
    unsigned mErrorCode;

};

class MyError : virtual public ErrorBase
{
    virtual char const **GetErrorTable()
    {
      static char const *data[] = {
        "no error",
        "error in MyClass",
        "specific error"
      };
      return data;
    }
};

Try something like: 尝试类似:

class ErrorBase 
{
  public:
    void SetError(unsigned errorCode)
    {
      mErrorCode = errorCode;
    }

    char const* Explanation(unsigned errorCode) const
    {
      return errorExplanations[errorCode];
    }

  private:
    char const** errorExplanations;
    unsigned mErrorCode;

};

class MyError : virtual public ErrorBase
{
  public:
    enum ErrorCodes {
      eNone,
      eGeneric,
      eMySpecificError
    };

    char const* child_strings[] = {"no error", "error in MyClass", "specific error"};

    MyError() 
    { 

      // I want this to refer to the parent class's attribute, 
      // such that when Explanation() is executed, it uses this
      errorExplanations = child_strings;
    }
    ~MyError() { }
};

Have your parent class contain only a pointer, have your child class create and initialize the array, and then make the pointer point to the array in your child's constructor. 让您的父类仅包含一个指针,让您的子类创建并初始化数组,然后使指针指向您孩子的构造函数中的数组。

Another option: make the Explanation(unsigned) const function virtual so that derived classes manage their own mechanism for looking up error messages: 另一个选择:将Explanation(unsigned) const函数Explanation(unsigned) const virtual以便派生类管理它们自己的机制以查找错误消息:

class ErrorBase 
{
public:
    virtual ~ErrorBase() { }

    void SetError(unsigned errorCode)
        : mErrorCode(errorCode)
    {
    }

    char const* Explanation() const { return this->Explanation(mErrorCode); }

    virtual char const* Explanation(unsigned errorCode) const
    {
        return errorExplanations[errorCode];
    }

private:
    unsigned mErrorCode;
};

class MyError : virtual public ErrorBase
{
public:
    enum ErrorCode {
        eNone = 0,
        eGeneric = 1,
        eMySpecificError = 2
    };

    MyError(ErrorCode c)
        : ErrorBase(static_cast<unsigned>(c))
    {
    }

    virtual ~MyError() { }

    virtual char const* Explanation(unsigned errorCode) const;
};

The error strings for MyError error codes are then compiled into an object file: 然后,将MyError错误代码的错误字符串编译到目标文件中:

// MyError.cpp
static const char* s_errorExplanations[] = {
        "no error",
        "error in MyClass",
        "specific error"
    };

char const* MyError::Explanation(unsigned errorCode) const
{
    return s_errorExplanations[errorCode];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM