简体   繁体   English

何时使用指针对数据成员进行分类?

[英]C++ - When to use pointers to class data members?

I've been coding in C++ and I was wondering if someone could help me with the general reason why we sometimes need to make pointers to class members and other times we don't. 我一直在用C ++进行编码,我想知道是否有人可以为我有时候有时需要指向类成员的指针的一般原因提供帮助,而其他时候却不需要。

For example if we are coding a Binary Tree 例如,如果我们正在编码二叉树

I implement it as 我实现为

class BinaryTree{

    BinaryTree * left;
    BinaryTree * right;
    int val;

    public:
        BinaryTree(int v) {left = NULL; right = NULL; val = v;}
        //implementation of any other neccessary functions
};

I use the BinaryTree pointers to left and right, because we can't do it without the pointer since BinaryTree does not exist at that point in time. 我在左侧和右侧使用BinaryTree指针,因为没有该指针我们无法做到这一点,因为BinaryTree在该时间点不存在。

Are there any other reasons to do this? 还有其他原因吗? Is there anyway around this? 有没有办法解决?

Also, if we put pointer member functions, will the implicit destructor handle the deletion of them? 另外,如果我们放置指针成员函数,隐式析构函数将处理它们的删除吗?

Thanks for your time. 谢谢你的时间。

This is a box: 这是一个盒子:

一个盒子,在这里代表一个结构

it has a volume of about one cubic meter, so it can only store objects that have total volume of one cubic meter. 它的体积约为一立方米,因此只能存储总体积为一立方米的对象。 And it definitely can't store two identical boxes as itself. 而且它绝对不能存储两个相同的盒子本身。 Note that each one of these two boxes would also need to contain two boxes like it, and so on, and so on. 请注意,这两个盒子中的每一个还需要包含两个类似的盒子,依此类推,依此类推。

This is a struct : 这是一个struct

struct BinaryTree {
    BinaryTree left;
    BinaryTree right;
    int val;
};

it has a finite size equal to sizeof(BinaryTree) , so it can only store objects that have total size less or equal to sizeof(BinaryTree) . 它具有等于sizeof(BinaryTree)的有限大小,因此它只能存储总大小小于或等于sizeof(BinaryTree) And it definitely can't store two values of type BinaryTree . 而且它绝对不能存储 BinaryTree类型的两个值。 Note that each one of these two values would also need to store two values like it, and so on, and so on. 请注意,这两个值中的每一个还需要存储两个类似的值,依此类推,依此类推。

Since the struct instances can't contain other instances of the same struct, and we need to define relations between them, and trees are definitely hierarchical, we use pointers here. 由于结构实例不能包含同一结构的其他实例,并且我们需要定义它们之间的关系,并且树肯定是分层的,因此我们在此处使用指针。

Note that the only thing that so called raw pointer to T (that is, T* ) does, is to point to T . 注意,所谓的指向T的原始指针(即T* )唯一要做的就是指向T Since pointing is the only task of such pointer, destruction won't destroy the pointed object, only the pointer . 由于指向是此类指针的唯一任务,因此销毁不会销毁指向的对象,而只会销毁指针

There exist types that behave like pointers, but also do other tasks, like managing lifetime of pointed object. 存在一些行为类似于指针的类型,但也执行其他任务,例如管理指向对象的生存期。 These are C++11's std::unique_ptr , and std::shared_ptr , and many others. 这些是C ++ 11的std::unique_ptrstd::shared_ptr等。 I highly recommend using them. 我强烈建议使用它们。

Objects often hold members that only need to be created based on run time conditions or parameters. 对象通常包含仅需要根据运行时条件或参数创建的成员。 You want to delay creation as late as possible. 您希望尽可能延迟创建。 This is a common case for using pointers. 这是使用指针的常见情况。

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

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