C++ C+中的堆栈成员与堆成员+;物体

C++ C+中的堆栈成员与堆成员+;物体,c++,pointers,object,C++,Pointers,Object,我事先道歉,因为我不想得到这样一个具体的答案。只要有一些见解就好了。下面的评论(连同准则)概述了一些观察结果、澄清和不确定性。这里的问题实际上是成员的生命周期 class SomeClass { int m_int; // primitive type. hardly ever see a pointer // For class instances, you always* seem to see this SomeOtherClass* m_some_other_class_inst

我事先道歉,因为我不想得到这样一个具体的答案。只要有一些见解就好了。下面的评论(连同准则)概述了一些观察结果、澄清和不确定性。这里的问题实际上是成员的生命周期

class SomeClass {
 int m_int; // primitive type. hardly ever see a pointer

 // For class instances, you always* seem to see this
 SomeOtherClass* m_some_other_class_instance_1;
 // and not this
 SomeOtherClass m_some_other_class_instance_2;

 // But lately, I've noticed that for std:: templates, it doesn't seem to be this
 vector<double>* m_vector_instance_1;
 // but rather this
 vector<double> m_vector_instance_2;
};

// So it got me thinking ...


void mainThread() {
  SomeClass* some_class_instance_1 = new SomeClass();
  // SomeClass instance on heap
  // So all its members (both <xx>_1 and <xx>_2) are on heap as well
  // Hence all its members will stay alive beyond the scope of this function (or do they?)

  SomeClass some_class_instance_2;
  // SomeClass instance on stack
  // So the only piece of data relating to SomeClass that's on the heap is what's pointed to by <xx>_1 members
  // But everything else will still stay alive within the scope of this function

  // In conclusion, using either case above, members of a SomeClass instance stay alive for their intended period
  // So are <xx>_1 members overkill?

  // Ah, ha, ha, ha, stayin' alive, stayin' alive ...
}
class-SomeClass{
int m_int;//基本类型。几乎看不到指针
//对于类实例,您似乎总是看到这一点
SomeOtherClass*m_SomeOther_class_instance_1;
//而不是这个
SomeOtherClass m_SomeOther_class_instance_2;
//但最近,我注意到对于std::templates,它似乎不是这样
向量*m_向量_实例_1;
//而是这个
向量m_向量_实例_2;
};
//所以我想。。。
void主线程(){
SomeClass*some_class_instance_1=新的SomeClass();
//堆上的某个类实例
//所以它的所有成员(包括_1和_2)也都在堆上
//因此,它的所有成员都将在该功能的范围之外保持活动状态(或者他们会吗?)
SomeClass some_class_instance_2;
//堆栈上的SomeClass实例
//因此,堆上与某个类相关的唯一数据块是_1成员所指向的
//但在这个函数的范围内,其他所有内容都将保持活动状态
//总之,使用上述任何一种情况,SomeClass实例的成员在其预期的时间段内保持活动状态
//那么"大学一年级"的成员是不是有点过火了??
//啊,哈,哈,哈,活着,活着。。。
}
在上下文方面,我们假设
SomeClass
不知道它周围的任何其他类,并且现在不希望传入/传出任何内容……因此构造函数可能只是用任何内容初始化其成员,而编写它的人不知道如何使用该类。唯一担心的是成员们还活着

我已经通读了这些线程,但它们并不十分相关:


你下面的假设实际上是错误的

class SomeClass {
 int m_int; // primitive type. hardly ever see a pointer
不,这取决于上下文或情况。你没有看到的东西并不意味着人们没有使用它

同样,这取决于您的要求

//但是最近,我注意到对于std::templates,它似乎不是这样的
向量*m_向量_实例_1;
//而是这个
向量m_向量_实例_2;
};
作用域和生存期是不同的术语,它不取决于是否使用指针

有关更多详细信息,请参见这些问题

如果我看到
int*m
我的第一反应是它是一个int数组

// For class instances, you always* seem to see this
 SomeOtherClass* m_some_other_class_instance_1;
 // and not this
 SomeOtherClass m_some_other_class_instance_2;
如果需要延迟加载对象,而不是在外部类运行时构造对象,则可能需要对对象进行堆分配。您可能这样做的另一个原因是多态性原因。如果
SomeOtherClass
是构造函数中的基类,则可以初始化不同的子类<代码>如果(某些条件)m_ptr=new Child1();else m_ptr=新子女2() 同样,您可能希望将其包装在一个独特的ptr中,以便销毁是自动的,并且不会泄漏

 // But lately, I've noticed that for std:: templates, it doesn't seem to be this
 vector<double>* m_vector_instance_1;
 // but rather this
 vector<double> m_vector_instance_2;
此对象及其成员将在超出范围时被销毁。虽然它的一些成员是指针。如果它们指向的是没有其他指针的堆分配成员,则会导致内存泄漏

  // In conclusion, using either case above, members of a SomeClass instance stay alive for their intended period
  // So are <xx>_1 members overkill?

<>这是C++,你会慢慢地死去,痛苦的死亡。< /P>你有问题吗?读代码里面的注释。其中一些是问题。不如说一些比“1个成员的杀伤力过大了吗?”更清楚、更具体的问题吧?事实上,这些问题非常清楚……但为了进一步澄清……我需要指向堆的指针,还是堆栈成员会在生命周期方面给我相同的完整性水平?堆分配的主要原因我不知道。所有权、跨其他对象共享对象、多态性、用于快速分配的内存池、延迟加载
init()。我知道我并没有看到每一段代码……只是简单地指出了“似乎”是常见的。因此,在函数之间传递成员的时间效率……这就是您需要作为堆上数据指针的成员的唯一原因吗?PS:在某些情况下,生命周期和范围可能非常相关。想想“局部非静态”变量……如我的例子所示。最后,有人真正阅读了我文章背后的意图。非常感谢。首选答案…直到出现更好的答案;)我已经编辑了我的问题以提供一些上下文,所以请相应地编辑你的问题+1对于缓慢而痛苦的死亡评论。
// For class instances, you always* seem to see this
 SomeOtherClass* m_some_other_class_instance_1;
 // and not this
 SomeOtherClass m_some_other_class_instance_2;
 // But lately, I've noticed that for std:: templates, it doesn't seem to be this
 vector<double>* m_vector_instance_1;
 // but rather this
 vector<double> m_vector_instance_2;
  SomeClass* some_class_instance_1 = new SomeClass();
  // SomeClass instance on heap
  // So all its members (both <xx>_1 and <xx>_2) are on heap as well
  // Hence all its members will stay alive beyond the scope of this function (or do they?)
  SomeClass some_class_instance_2;
  // SomeClass instance on stack
  // So the only piece of data relating to SomeClass that's on the heap is what's pointed to by <xx>_1 members
  // But everything else will still stay alive within the scope of this function
  // In conclusion, using either case above, members of a SomeClass instance stay alive for their intended period
  // So are <xx>_1 members overkill?
  // Ah, ha, ha, ha, stayin' alive, stayin' alive ...