简体   繁体   English

在C ++中混合接口和嵌套类

[英]Mixing interfaces and nested classes in C++

I am pretty new to C++. 我对C ++很陌生。 Today, I am experiencing some issues in mixing nested classes and interfaces. 今天,我在混合嵌套类和接口时遇到一些问题。

I wrote a small (useless) program that will be more effective at explaining my issue than long sentences: 我写了一个小程序(无用),比长句子更能有效地解释我的问题:

#include <iostream>
#include <vector>

class SomeInterface {
  public:
    virtual int SomeMethod() = 0;

    class reference {
      public:
        virtual operator int() const = 0;
        virtual reference& operator=(int x) = 0;
    };

    virtual reference operator[](unsigned int pos) = 0;
};

class A : public SomeInterface {
  public:
    A(unsigned int size) { vec_.resize(size, 0); }
    int SomeMethod() { return 1; }

    class reference : public SomeInterface::reference {
      public:
        reference(std::vector<int>::reference ref) : ref_(ref) { }
        operator int() const { return (int) this->ref_; }
        reference& operator=(int x) { this->ref_ = x; return *this; }
      private:
        std::vector<int>::reference ref_;
    };

    reference operator[](unsigned int pos) {
      return reference(this->vec_[pos]);
    };

  private:
    std::vector<int> vec_;
};

int main() {
  A a(10);
  a[5] = 42;
  std::cerr << a[5] << std::endl;
  return 0;
}

Here, the program compiles fine if I remove the line virtual reference operator[](unsigned int pos) = 0; 在这里,如果删除行virtual reference operator[](unsigned int pos) = 0; ,则程序可以正常编译virtual reference operator[](unsigned int pos) = 0; in the interface. 在界面中。 However, I would like the array subscript operator to be part of the interface. 但是,我希望数组下标运算符成为接口的一部分。

The error message thrown by G++ is invalid abstract return type for member function 'virtual SomeInterface::reference SomeInterface::operator[](unsigned int)' . G ++抛出的错误消息invalid abstract return type for member function 'virtual SomeInterface::reference SomeInterface::operator[](unsigned int)'invalid abstract return type for member function 'virtual SomeInterface::reference SomeInterface::operator[](unsigned int)'

I do understand why it fails. 我知道为什么会失败。 But I can't figure out any way to make something like this work. 但是我不知道有什么办法可以做这样的事情。 Can anybody explain why I am doing (or thinking) wrong? 谁能解释我为什么做错(或思考)错误?

You can not create objects of type SomeInterface::reference , since it is a pure abstract class, and that is what the compiler told you. 您不能创建SomeInterface::reference类型的对象,因为它是纯抽象类,而这正是编译器告诉您的。

You need to return a reference (or a pointer) to such class. 您需要返回对该类的引用(或指针)。 Like this : 像这样 :

virtual reference& operator[](unsigned int pos) = 0;

but then : 但是之后 :

  1. in derived classes, you shouldn't change the signature of the pure virtual methods. 在派生类中,您不应更改纯虚方法的签名。 It should stay virtual SomeInterface::reference& operator[](unsigned int pos) 它应该保持virtual SomeInterface::reference& operator[](unsigned int pos)
  2. you can not return reference to a temporary object 您不能返回对临时对象的引用

btw take care how you create objects of such classes. 顺便说一句,请注意如何创建此类的对象。 They do not have virtual destructors. 他们没有虚拟析构函数。

Basically you can't return a reference to something that can never exist. 基本上,您无法返回对永远不存在的事物的引用。 However, you could use a pointer to an abstract class. 但是,您可以使用指向抽象类的指针。 That pointer will ultimately only be able to point to an instance of a derived class. 该指针最终将只能指向派生类的实例。

It is not clear exactly what you are trying to do. 目前尚不清楚您正在尝试做什么。 But you might look into Creation Patterns to find something close to what you need 但是,您可能会研究“创建模式”以找到与您所需内容接近的内容

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

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