简体   繁体   English

C ++ 14忽略接口getter中的返回类型,但在实现中指定它

[英]C++14 ignore return type in interface getter but specify it in implementation

I have three classes of objects: 我有三类对象:

  • class Foo: has a mesh, and I need to get that mesh; Foo类:有一个网格,我需要得到那个网格;
  • class Bar: is a Foo, but has some further capabilities which Foo doesn't have; class Bar:是Foo,但具有Foo没有的其他功能;
  • class Baz: is a Foo, but has another completely independent set of capabilities which neither Foo nor Bar have. Baz类:是Foo,但是具有Foo和Bar都没有的另一套完全独立的功能。

All three classes need to have a way to give me their mesh which, however, can be implemented in many ways, of which I need (at the moment I can't see another way) to use at least 2 different ones, which are MeshTypeA and MeshTypeB. 这三个类都需要有一种方法给我它们的网格,但是,可以用很多方法来实现它们,而我需要(目前我看不到另一种方法)至少使用两种不同的方法,分别是MeshTypeA和MeshTypeB。

I would like to have a common interface for different implementations of the same concept (getMesh), however, I can't use auto in a virtual method. 我想为同一个概念的不同实现(getMesh)提供一个公共接口,但是,我不能在虚拟方法中使用auto。 I'm lacking the facility to make the code have sense. 我缺乏使代码有意义的工具。 I would like to have: 我想拥有:

class Foo
{
public:
  virtual ~Foo() = 0;
  virtual auto getMesh() const = 0;  // doesn't compile
};

class Bar : public Foo
{
public:
  virtual ~Bar() = 0;
  virtual auto getMesh() const = 0;  // doesn't compile
  // other virtual methods
};

class ConcreteFooWhichUsesA : public Foo
{
public:
  ConcreteFooWhichUsesA();
  ~ConcreteFooWhichUsesA();
  auto getMesh() const override {return mesh_;};

private:
  MeshTypeA mesh_;
};

class ConcreteBarWhichUsesB : public Bar
{
public:
  ConcreteBarWhichUsesB();
  ~ConcreteBarWhichUsesB();
  auto getMesh() const override {return mesh_;};
  // other implementations of virtual methods

private:
  MeshTypeB mesh_;
};

MeshTypeA and MeshTypeB are not exclusive to Foo, Bar, or Baz, which is to say all three could have both types of mesh. MeshTypeA和MeshTypeB不是Foo,Bar或Baz专用的,也就是说,这三种都可以同时具有两种类型的网格。 However I really don't care for which MeshType I get when I later use it. 但是,我以后以后再使用它时,我真的不在乎要使用哪种MeshType。

Do I need to wrap MeshTypeA and MeshTypeB in my own MeshType? 我是否需要在自己的MeshType中包装MeshTypeA和MeshTypeB? Is it a matter of templating the MeshType? 这是模板网格类型的问题吗? I believe there is a way, however related questions aren't helping or I can't formulate my question in a meaningful enough way. 我相信有办法,但是相关的问题无济于事,或者我无法以足够有意义的方式提出我的问题。

I have also found this where the author uses a Builder class and decltype, but I don't have such a class. 在作者使用Builder类和decltype的地方,我也发现了这一点 ,但是我没有这样的类。 Maybe that would be it? 也许是这样吗? Do I need a MeshLoader sort of class as an indirection level? 我是否需要将MeshLoader类作为间接级别?

If your MeshTypes all have a common (abstract) base class, then you can just return (a pointer or reference to) that in the virtual function defintions, and the derived classes can then return their concrete mesh types, and all will be well. 如果您的MeshTypes都有一个通用的(抽象的)基类,那么您只需在虚函数定义中返回(指针或对其的引用),然后派生的类就可以返回其具体的网格类型,一切都会好起来的。 If you have code that can work on any mesh type, it is going to need that abstract base anyways. 如果您的代码可以在任何网格类型上使用,那么无论如何都将需要该抽象基础。

If your MeshTypes do not all have a common base class, why even have a getMesh method in Foo at all? 如果您的MeshType都没有一个通用的基类,为什么还要在Foo中甚至有一个getMesh方法呢? Remove it and give each of the concrete classes it's own getMesh method that doesn't override (and has nothing in particular to do with the meshes in any other concrete class). 删除它,并为每个具体类提供一个自己的getMesh方法,该方法不会覆盖(并且与其他任何具体类中的网格无关)。

A function's return type is part of its interface. 函数的返回类型是其接口的一部分。 You can't just change it willy-nilly. 您不能随便改变它。 More specifically, you cannot have a base class virtual method return one thing while an overridden version returns another. 更具体地说,您不能让基类虚拟方法返回一件事,而被覆盖的版本则返回另一件事。 OK, you can , but only if the derived version's return type is convertible to the base class return type (in which case, calling through the base class function will perform said conversion on the overriding method's return type). 是的,您可以 ,但是仅当派生版本的返回类型可转换为基类的返回类型时(在这种情况下,通过基类函数进行的调用将对重写方法的返回类型执行上述转换)。

C++ is a statically typed language; C ++是一种静态类型的语言。 the compiler must know what an expression evaluates to at compile time. 编译器必须在编译时知道表达式的计算结果。 Since polymorphic inheritance is a runtime property (that is, the compiler is not guaranteed to be able to know which override will be called through a base class pointer/reference), you cannot have polymorphic inheritance change compile-time constructs, like the type of a function call expression. 由于多态继承是运行时属性(也就是说,不能保证编译器能够知道将通过基类指针/引用调用哪个覆盖),因此您不能像使用的类型那样使用多态继承更改编译时构造。函数调用表达式。 If you call a virtual method of a base class instance, the compiler will expect this expression to evaluate to what that base class's method returns. 如果您调用基类实例的虚拟方法,则编译器将期望此表达式求值该基类的方法返回的值。

Remember: the point of polymorphic inheritance is that you can write code that doesn't know about the derived classes and have it still work with them. 请记住:多态继承的要点是,您可以编写对派生类一无所知的代码,并使它仍然可以与它们一起工作。 What you're trying to do violates that. 您尝试执行的操作违反了该要求。

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

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