简体   繁体   English

C ++成员函数需要指针,不好的做法是通过引用传递?

[英]C++ member function requires pointer, bad practice to pass by reference?

If a C++ class member function requires a pointer to an object as an argument, is it considered bad practice to pass by reference? 如果C ++类成员函数需要一个指向对象的指针作为参数,通过引用传递是否被认为是不好的做法?

The following code, for example, will work, however without the pass by reference it becomes a dangerous code, and will lead to catastrophic errors at runtime. 例如,以下代码将起作用,但是,如果不通过引用传递,它将成为危险代码,并在运行时导致灾难性错误。

class ClassA
{
public:
    void SetPointer(const ClassB& classb) // Remove 1 ampersand and serious errors will occur
    {
        if(ptr_to_classb == nullptr) // Initialized to nullptr in constructor
            ptr_to_classb = &classb;
        else
            throw(...); // Throw some error
    }

private:
    ClassB* ptr_to_classb;
}

Consider if passing by value, and a copy of the argument was made, that this would be disastrous when dereferencing at a later time. 考虑是否按值传递,并且复制了参数,那么在以后取消引用时,这将是灾难性的。

The alternative is this: 替代方法是这样的:

class ClassA
{
public:
    void SetPointer(const ClassB* const classb)
    {
        if(ptr_to_classb == nullptr) // Initialized to nullptr in constructor
            ptr_to_classb = (ClassB*)(classb);
        else
            throw(...); // Throw some error
    }

private:
    ClassB* ptr_to_classb;
}

I like consistency, to defaulted to the first type, however I suspect that the second form is considered to be better practice. 我喜欢一致性,默认为第一种,但是我怀疑第二种形式被认为是更好的做法。 Is this the case? 是这样吗

My view is that if passing a null argument to the method is a valid thing to do (ie the logic that the method executes would be valid with a null pointer), then use a pointer. 我的观点是,如果将null参数传递给该方法是正确的做法(即该方法执行的逻辑在使用null指针的情况下将是有效的),则应使用指针。 If the argument should never be null then use a reference. 如果参数永远不能为null,则使用引用。

In your case this depends on whether it is valid for ClassA::ptr_to_classb to be null. 在您的情况下,这取决于ClassA :: ptr_to_classb是否为null是否有效。 Since you throw if ptr_to_classb is already set (meaning you don't ever want to change what it points to) you might even want to conside storing a reference instead and passing that in the constructor of ClassA, getting rid of ClassA::SetPointer. 因为如果抛出ptr_to_classb已经设置(意味着您永远不想更改它指向的内容),您甚至可能要考虑存储一个引用,并将其传递给ClassA的构造函数,从而摆脱ClassA :: SetPointer。

There are some other opinions on reference vs pointer here as well . 有上参考VS指针一些其他的意见在这里也

Well, both approaches are correct and fine but in your case it will be probably better to go with pointers, since a reference variable can only be assigned a value at initialization unlike pointers. 嗯,这两种方法都是正确且正确的,但在您的情况下,最好使用指针,因为与变量不同,只能在初始化时为参考变量分配一个值。 With the same pointer you could later pass a different class object. 使用相同的指针,您以后可以传递不同的类对象。

Your method just sets a field of your object, so it seems you want to use the type of the field (which is pointer, not reference). 您的方法只是设置对象的一个​​字段,因此您似乎想使用字段的类型(它是指针,而不是引用)。 You wrote 你写了

I like consistency, to defaulted to the first type 我喜欢一致性,默认为第一种

which, I guess, refers to the rule "use references when possible; use pointers oterwise". 我猜这是指规则“尽可能使用引用;否则使用指针”。 I think your case is an exception from this rule, because the declaration 我认为您的情况是该规则的例外,因为声明

void do_stuff(ClassA& object)

usually means "do stuff on the object, and forget about it", and your case is different. 通常意味着“在对象上做事,然后忘记它”,而您的情况则有所不同。

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

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