简体   繁体   English

指针作为成员或引用作为成员

[英]Pointer as member or Reference as member

I need one clarification in c++ linux. 我需要在c ++ linux中做一个澄清。

I have class C1 and another class C2 . 我有class C1和另一class C2 C1 will have the reference of C2 . C1将具有C2的参考。

class C1
{

   C2 &obj ;

}

i am thinking of two choices here , 我在考虑两个选择,

  1. Directly holding the reference of C2 as C2 &obj; 直接将C2的引用保持为C2 &obj;
  2. Creating the pointer of C2 , as c2* obj; 创建C2的指针,如c2* obj;

Which is good ? 哪个好? what is the difference in it ? 它有什么区别? when choose either? 何时选择?

Avoid using a reference member as much as possible. 避免尽可能使用引用成员。

The same differences as that of references and pointers apply here, 这里与引用和指针的区别相同,
If you have a reference member then it must be initialized at the time of creation of your class object you cannot have a lazy initialization as in case of pointer member because references cannot be NULL and cannot be reseated, while a pointer member can be made to point to a C2 instance lazily as and when required. 如果你有一个引用成员,那么必须在创建类对象时初始化它,你不能像指针成员一样进行延迟初始化,因为引用不能为NULL而且不能重新引用,而指针成员可以使用在需要时懒洋洋地指向C2实例。

Also, note that there are other side effects as well, Once you have a reference member the compiler will not generate the copy assignment operator( = ) & You will have to provide one yourself, It is cubersome to determine what action your = operator shall take in such a case. 此外,请注意还有其他副作用,一旦你有一个引用成员,编译器将不会生成复制赋值运算符( = )&你必须自己提供一个,它是cubersome来确定你的=运算符应该采取什么行动接受这种情况。

For most practical purposes(unless you are really concerned of high memory usage due to C2 size) just holding an instance of C2 as member of C1 should suffice, instead of pointer or reference member, that saves you a whole lot of worrying about other problems which reference/pointer members bring along though at expense of extra memory usage. 最实用的目的(除非你真的关心内存使用率很高的,由于C2的大小)只是抱着的一个实例C2作为成员C1应该足够了,而不是指针或引用成员,可为您节省了一大堆担心其他问题哪个引用/指针成员带来了,但代价是额外的内存使用量。

If you must, use a pointer make sure you use a smart pointer instead of a raw pointer, that would make your life much easier with pointers. 如果必须,使用指针确保使用智能指针而不是原始指针,这将使指针生活更轻松。

a pointer has two main advantages to references. 指针对引用有两个主要优点。 the pointer can be null and a pointer can be moved (with ++ operator for example). 指针可以为null,并且可以移动指针(例如,使用++运算符)。 The moving doesn't help you here. 移动对你没有帮助。 If your class C2 is not known when you instantiate C1 then you should use a pointer because you have no way to initialize the reference, but the pointer can be initialized to null. 如果在实例化C1时未知类C2,则应使用指针,因为无法初始化引用,但指针可以初始化为null。

Like Als said, i would also recommend using pointers when the C2 instance may change. 就像Als所说,我也建议在C2实例可能改变时使用指针。 You can uses references in this case but i think pointers are a 'cleaner' solution. 你可以在这种情况下使用引用,但我认为指针是一个“更清洁”的解决方案。

Neither is good. 两者都不好。 If you have any pointer or reference member variable pointing at something allocated outside your class, it most likely means that the program design is flawed somehow. 如果你有任何指针引用成员变量指向你班级之外分配的东西,那么很可能意味着程序设计在某种程度上是有缺陷的。

One case I can come up with where it is justified, is when you have some sort of peculiar, external garbage collector functionality. 我可以提出一个合理的案例,就是当你拥有某种特殊的外部垃圾收集器功能时。 (For example C++ Builder's automatic allocation/deallocation of VCL components on the heap.) (例如,C ++ Builder在堆上自动分配/释放VCL组件。)

Another case is where it could be valid, is when you are emulating a C struct with a C++ class, or when writing a private class inside another class. 另一种情况是它可能是有效的,就是当你用C ++类模拟C结构时,或者在另一个类中编写私有类时。

But then most likely, the need to point at something outside the class comes from flawed program design. 但最有可能的是,需要指出课外的东西来自有缺陷的程序设计。 I would think twice of why my code needs to do this, and at the very least try to add as much const correctness at possible to the pointer, ie const *c2 const obj; 我会考虑为什么我的代码需要这样做,并且至少尝试在指针上添加尽可能多的const正确性,即const *c2 const obj;

If you have peculiar member pointers like this, you must also most likely manually write a copy constructor, assignment operator and destructor for your class. 如果您有这样的特殊成员指针,您还必须手动为您的类手动编写复制构造函数,赋值运算符和析构函数。

Edit: The rationale for why this is bad practice is the concept called private encapsulation , which is one of the cornerstones of the program design concept called object-oriented design . 编辑:为什么这是不好的做法的基本原理是称为私有封装的概念,它是程序设计概念的基石之一,称为面向对象设计

Which one you are going to use, depends on what you exactly need. 您要使用哪一个取决于您的确切需要。

For example, you can use a reference member variable if you need some kind of dependency injection. 例如,如果需要某种依赖注入,则可以使用引用成员变量。 Even then, you can use pointers. 即使这样,你也可以使用指针。 However, keep in mind that classes with reference members should generally be non-copyable. 但是,请记住,具有引用成员的类通常应该是不可复制的。

In most cases, you should either use pointers or value member variables. 在大多数情况下,您应该使用指针或值成员变量。

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

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