在C ++中使用指针总是坏吗? [英] Is using pointers in C++ always bad?

查看:144
本文介绍了在C ++中使用指针总是坏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知要避免在C ++中使用指针。似乎我不能避免他们,但在代码我试图写,或者我错过了其他伟大的C ++功能。

I was told to avoid using pointers in C++. It seems that I can't avoid them however in the code i'm trying to write, or perhaps i'm missing out on other great C++ features.

我希望创建一个包含另一个类(class2)作为数据成员的类(class1)。然后我想要class2知道关于class1,并能够与它沟通。

I wish to create a class (class1) which contains another class (class2) as a data member. I then want class2 to know about class1 and be able to communicate with it.

我可以引用class1作为class2中的成员,但这意味着我需要在class2的构造函数中提供对class1的引用作为参数,并使用initialiser我不想要的列表。我试图这样做而不需要构造函数来做。

I could have a reference to class1 as a member in class2 but that then means I need to provide a reference to class1 as a parameter in the constructor of class2 and use initialiser lists which I don't want. I'm trying to do this without needing the constructor to do it.

我希望class2有一个名为Initialise的成员函数,它可以参考class1 ,但这似乎不可能不使用指针。人们会在这里推荐什么?提前感谢。

I would like for class2 to have a member function called Initialise which could take in the reference to class1, but this seems impossible without using pointers. What would people recommend here? Thanks in advance.

代码完全简化只是为了获得主要想法:

The code is completely simplified just to get the main idea across :

class class1
{
    public:
        InitialiseClass2()
        {
            c2.Initialise(this);
        }

    private:
        class2 c2;

};

class class2
{
    public:
        Initialise(class1* c1)
        {
            this->c1 = c1;
        }

    private:
        class1* c1;

};


推荐答案


使用指针

this seems impossible without using pointers

这不正确。实际上,为了处理对其他对象的引用,引用

That is incorrect. Indeed, to handle a reference to some other object, take a reference into a constructor:

class class2
{
    public:
        class2(class1& c1)
           : c1(c1)
        {}

    private:
        class1& c1;
};

这里的关键是初始化,这是否可能取决于您是否可以摆脱初始化功能并结算到RAII(请执行!)。之后,这是否实际上是个好主意取决于您的使用案例;现在,你几乎可以通过使用智能指针类型之一来使所有权和生命周期语义更清楚。即使只是一个 std :: weak_ptr

The key here is to initialise, not assign, the reference. Whether this is possible depends on whether you can get rid of your Initialise function and settle into RAII (please do!). After that, whether this is actually a good idea depends on your use case; nowadays, you can almost certainly make ownership and lifetime semantics much clearer by using one of the smart-pointer types instead — even if it's just a std::weak_ptr.

无论如何,更一般地说。

Anyway, speaking more generally.

指针总是错误?否,当然不是。我几乎可以说,管理动态记忆本身是总是坏,但我不会概括。

Are pointers "always" bad? No, of course not. I'd almost be tempted to say that managing dynamic memory yourself is "always" bad, but I won't make a generalisation.

如果你避免?是的。

Should you avoid them? Yes.

不同之处在于,后者是指导您远离手动内存管理的指南,是一种尝试禁止。

The difference is that the latter is a guideline to steer you away from manual memory management, and the former is an attempted prohibition.

这篇关于在C ++中使用指针总是坏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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