简体   繁体   English

如果一个类将要基于指针,那么内部对象是否必须基于指针?

[英]If a class are going to be pointer based an inner object have to be pointer based or not?

I have a class A and class B. I can have a private or public class_B object inside class A. (myclassB) Ok, I create my class_A objects on the heap: 我有一个A类和一个B类。我可以在A类中拥有一个私有或公共class_B对象。(myclassB)好吧,我在堆上创建了class_A对象:

classA = new classA();
classA->myclassB.property =1;  // in case myclassB is public.
classA->myclassB.function_x(33); // in case myclassB is public.

First ... How can I call myclassB ? 首先...我怎么称呼myclassB? stacky-clas ? 层叠式 fixed_class ? fixed_class? Second... Am I to have any problem to do this ? 第二...我这样做有什么问题吗? Is best ? 是最好的 ? It does not matter? 不要紧? Thanks 谢谢

No. If I have a class that internally has a normal object (no pointer), than it doesn't matter whether the outer object is a pointer or not, the inner object always gets accessed normally. 否。如果我有一个内部有普通对象(没有指针)的类,那么无论外部对象是否是指针都无所谓,内部对象总是可以正常访问。

A stack_a, *heap_a;
heap_a = new A();

/* Inner object always uses normal access */
stack_a.b.test()
heap_a->b.test()

The opposite is true too, if a class internally uses a pointer to an object then it doesn't matter whether the outer object is a pointer or not, the inner object always uses pointer access. 反之亦然,如果类在内部使用指向对象的指针,则不管外部对象是否为指针,内部对象始终使用指针访问。

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

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