如何在C ++中实现类组成? [英] How to implement class composition in C++?

查看:118
本文介绍了如何在C ++中实现类组成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确,我们至少有2种不同的实现组合的方式。 (使用智能指针的实现是排除的情况,为简单起见,我不使用STL几乎没有想要学习它。)

If I understand correctly we have at least 2 different ways of implementing composition. (Implementation with smart pointers is excluded case for simplicity. I don't use STL almost and have no desire to learn it.)

让我们看看维基百科示例

class Car
{
  private:
    Carburetor* itsCarb;
  public:   
    Car() {itsCarb=new Carburetor();}
    virtual ~Car() {delete itsCarb;}
};

因此,这是一种方法 - 我们有一个指向对象的指针会员。
可以将其重写为:

So, it's one way - we have a pointer to object as private member. One can rewrite it to look like this:

class Car
{
  private:
    Carburetor itsCarb;
};

在这种情况下,我们有一个对象 (顺便说一句,调用这个实体作为对象我从术语的角度写?)

In that case we have an object itself as private member. (By the way, calling this entity as object am I write from the terminology point of view?)

在第二种情况下,不一定要隐式调用默认构造函数如果一个需要调用非默认构造函数,它可以在初始化列表中做)和析构函数。但这不是一个大问题...

In the second case it is not obligatory to implicitly call default constructor (if one need to call non-default constructor it's possible to do it in initializer list) and destructor. But it's not a big problem...

当然,在某些方面,这两种情况有所不同。例如,在第二种情况下禁止从Car类的const方法调用Carburetor实例的非const方法...

And of course in some aspects these two cases differ more appreciably. For example it's forbidden to call non-const methods of Carburetor instance from const methods of Car class in the second case...

有没有任何规则来决定一个使用?我错过了什么吗?

Is there any "rules" to decide which one to use? Am I missed something?

提前感谢。

推荐答案


在这种情况下,我们有一个对象本身作为私有成员。 (顺便说一句,调用这个实体作为对象我从术语的角度写?)

In that case we have an object itself as private member. (By the way, calling this entity as object am I write from the terminology point of view?)

是的,你可以说对象或类的实例。

Yes you can say "an object" or "an instance" of the class.

你还可以谈论包括数据成员按值而不是通过指针 和按值是讨论传递参数的正常方式,因此我预计人们会理解这些术语应用于数据成员)。

You can also talk about including the data member "by value" instead of "by pointer" (because "by pointer" and "by value" is the normal way to talk about passing parameters, therefore I expect people would understand those terms being applied to data members).


有没有任何规则来决定使用哪一个?我错过了什么?

Is there any "rules" to decide which one to use? Am I missed something?

如果实例由多个容器共享,则每个容器应该通过指针而不是值;例如,如果Employee有一个Boss实例,如果几个Employee实例共享同一个Boss,则包含Boss指针。

If the instance is shared by more than one container, then each container should include it by pointer instead of value; for example if an Employee has a Boss instance, include the Boss by pointer if several Employee instances share the same Boss.

如果数据成员的生命周期不是与容器的生命周期相同,然后通过指针包括它:例如,如果数据成员在容器之后被实例化,或者在容器之前被销毁,或者在容器的生命周期内被破坏和重新创建,或者如果它

If the lifetime of the data member isn't the same as the lifetime of the container, then include it by pointer: for example if the data member is instantiated after the container, or destroyed before the container, or destroyed-and-recreated during the lifetime of the container, or if it ever makes sense for the data member to be null.

另一次当你必须通过指针(或引用)而不是按值来包含的时候,数据成员的类型是一个抽象的基类。

Another time when you must including by pointer (or by reference) instead of by value is when the type of the data member is an abstract base class.

使用指针包含的另一个原因是,可能允许你改变数据成员的实现,而不需要重新编译容器。例如,如果Car和Carburetor在两个不同的DLL中定义,您可能希望通过指针包括Carburetor:因为那样您可以通过安装不同的 Carburetor.dll ,而不重建 Car.dll

Another reason for including by pointer is that that might allow you to change the implementation of the data member without recompiling the container. For example, if Car and Carburetor were defined in two different DLLs, you might want to include Carburetor by pointer: because then you might be able to change the implementation of the Carburetor by installing a different Carburetor.dll, without rebuilding the Car.dll.

这篇关于如何在C ++中实现类组成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆