访问“这个”具体类的指针从接口

编程入门 行业动态 更新时间:2024-10-26 04:21:10
本文介绍了访问“这个”具体类的指针从接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

写入测试后,我确定接口中的此指针不等于 this 指针的具体类,意味着我不能只使用C风格的演员。

class AbstractBase {... }; 类AnInterface { public: AnInterface(){...} //需要AbstractBase * here 〜virtual AnInterface(){...} / and here }; 类具体:public AbstractBase,public AnInterface {};

我的接口需要一个基类指针指向在构造函数和析构函数中继承它的具体类,以便

继承接口的每个具体对象首先需要继承抽象基类,它始终是布局中的第一个。

对于构造函数来说并不难,我可以在接口构造函数中添加一个指针,并从具体类中传递 this 。但是析构函数没有任何参数,所以我在黑暗中。

我想出的解决方案到目前为止带来了开销:

1 - 将指针存储在析构函数中使用的接口中 - 添加一个内存开销指针

class AnInterface { public: AnInterface(AbstractBase * ap){...} 〜virtual AnInterface(){...} // and here private: AbstractBase * aPtr; } ... Concrete():AnInterface(this){}

$ b b

2 - 在接口中创建一个抽象方法并实现它在实体类中返回 this - 在虚拟调用的间接开销中添加

class AnInterface { virtual AbstractBase * getPtr()= 0; } 类具体:public AbstractBase,public AnInterface { AbstractBase * getPtr(){return this; } };

3 - dynamic_cast

解决方案

是否有更有效的方法来实现?基本类和接口之间的解耦是真正需要的,解决方案1和2都有可容忍的开销,当然没有什么是当代硬件上的问题。

说接口是设计用于在基类中提供的功能,那么也许解耦不是一件好事。

我的意思是如果问题是继承了所有继承基类的多个接口,或继承的可怕的钻石问题,你可以简单地使用虚拟继承。

After writing a test, I determined that the this pointer in an interface is not equal to the this pointer of the concrete class, meaning I can't just use a C-style cast on it.

class AbstractBase {...}; class AnInterface { public: AnInterface() {...} // need AbstractBase * here ~virtual AnInterface() {...} // and here }; class Concrete : public AbstractBase, public AnInterface {};

My interface needs a base class pointer to the concrete class inheriting it in the constructor and destructor in order to handle interface related registration and deregistration.

Every concrete object that inherits the interface needs to inherit the abstract base class first, it is always first in the layout.

For the constructor it is not all that hard, I can add a pointer in the interface constructor and pass this from the concrete class. But the destructor doesn't have any parameters, so I am in the dark there.

The solutions I came up with so far come with overhead:

1 - store the pointer in the interface to be used in the destructor - adds in one pointer worth of memory overhead

class AnInterface { public: AnInterface(AbstractBase * ap) {...} ~virtual AnInterface() {...} // and here private: AbstractBase * aPtr; }; ... Concrete() : AnInterface(this) {}

2 - create an abstract method in the interface and implement it to return this in the concrete class - adds in the overhead of indirection for the virtual call

class AnInterface { virtual AbstractBase * getPtr() = 0; }; class Concrete : public AbstractBase, public AnInterface { AbstractBase * getPtr() { return this; } };

3 - dynamic_cast is even worse

Is there a more efficient way to achieve this?

解决方案

IMO if decoupling between the base class and the interface is really needed, both solution 1 and 2 have tolerable overheads, certainly nothing that will be a problem on contemporary hardware.

But since you say that the interface is designed to work with the functionality, provided in the base class, then maybe the decoupling is not a good thing.

I mean if the problem is with inheriting multiple interfaces which all inherit the base class, or the "dreaded diamond" problem with inheritance, you can simply use virtual inheritance.

更多推荐

访问“这个”具体类的指针从接口

本文发布于:2023-11-12 09:41:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   接口

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!