简体   繁体   English

C ++中一个类的多个接口

[英]Multiple Interfaces to a Class in C++

I have three classes that interact as follows. 我有三个互动如下的类。 Class A contains a private member of type Class B . Class A包含类型Class B的私有成员。 It also contains a method to which an object of type ClassC is passed. 它还包含将ClassC类型的对象传递给的方法。 This method then calls a method on ClassC , passing to it a particular interface ( ClassBInterface1 ) of its member of type ClassB : 然后,此方法在ClassC上调用一个方法,并将其类型为ClassB成员的特定接口( ClassBInterface1 )传递给该方法:

 ClassA
 {
     void Foo(ClassC ObjectC)
     {
         ObjectC.Bar((ClassBInterface1) ObjectB);
     }
     ClassB ObjectB;
 }

My question is: ClassA does not need to access the methods of ClassB defined in Interface1 . 我的问题是: ClassA不需要访问Interface1定义的ClassB方法。 Therefore, in my view, it would be more elegant if the member of ClassA was of type ClassBInterface2 , rather than ClassB . 因此,在我看来,如果ClassA的成员类型为ClassBInterface2而不是ClassBClassBInterface2了。 Is it possible to do this, while still passing B to C under Interface1 ? 是否仍可以在Interface1下将B传递给C的同时这样做?

The only way I can think of is to typecast ClassBInterface2 to ClassB and back to ClassBInterface1 in the Bar method in ClassA . 我能想到的唯一方法是将ClassBInterface2转换为ClassB ,然后在ClassA的Bar方法中转换回ClassBInterface1

Is this the best way to do it? 这是最好的方法吗? Or should I just leave it as it is? 还是应该保留原样?

Thanks a lot for any help. 非常感谢您的帮助。

If you define ObjectB as a ClassBInterface2 it won't be possible to convert it to ClassBInterface1 at runtime because it's internal structure won't be known. 如果定义ObjectB作为ClassBInterface2它是不可能将其转换为ClassBInterface1 ,因为它不会被称为内部结构在运行时。

Your way is the best one to do it but you can do a little modification. 您的方式是最好的方式,但是您可以做一些修改。 You don't need to do a explicit cast from ClassB to ClassBInterface1 while calling ObjectC.Bar because the compiler will do it for you. 在调用ObjectC.Bar时,您不需要从ClassB显式转换为ClassBInterface1 ,因为编译器会为您完成此转换。

If class B is defined as follows: 如果class B的定义如下:

ClassB : public ClassBInterface1, ClassBInterface2
{

  /*Class methods and attributes*/

}

you can just do the following while calling the Bar function on the ObjectC (assuming objectB is defined as ClassB ) 您可以在objectB上调用Bar函数时执行以下操作(假设objectB定义为ClassB

ObjectC.Bar(ObjectB);

C++ has a great feature for this called "forward declarations". C ++对此有一个伟大的功能,称为“转发声明”。 Basically, for any parts of your code that don't need to know the details of a class, you can simply pass around a reference. 基本上,对于代码中不需要了解类详细信息的任何部分,只需传递引用即可。 Only when you want to call member methods (including constructors and destructors) do you need to have the full class definition. 仅当您要调用成员方法(包括构造函数和析构函数)时,才需要具有完整的类定义。

#include "ClassC.h"
class ClassB;

class ClassA
{
public:
    void foo(ClassC& objectC)
    {
       objectC.bar(_objectB);
    }

protected:
    ClassB& _objectB;
};

Note that we include a header for ClassC because we need to call one of his methods. 请注意,我们包括ClassC的标头,因为我们需要调用他的方法之一。

Note that we forward declare ClassB and only hold a reference because we don't really care what he is. 请注意,我们转发声明ClassB并仅保留一个引用,因为我们并不在乎他是什么。

Note finally that ClassA can't be instantiated currently, because somehow the reference to _objectB has to be set to something. 最后要注意,ClassA当前无法实例化,因为以某种方式必须将对_objectB的引用设置为某种。 For example, a constructor: 例如,一个构造函数:

public ClassA(ClassB& objectB)
    : _objectB(objectB)
{}

ClassA now only holds on to whatever reference was given to him on construction. 现在,ClassA仅保留在构造上给予他的任何参考。

Based on your use of the term "interface" in your question, I assume you may have a class hierarchy. 根据您对问题中“接口”一词的使用,我认为您可能具有类层次结构。 This answer can easily be extended to such a hierarchy. 这个答案可以很容易地扩展到这样的层次结构。 But the important point here is that concrete types always require a class definition, while simple reference object only require a forward declaration. 但这里的重点是,具体类型始终需要类定义,而简单引用对象仅需要前向声明。

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

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