简体   繁体   English

C ++构造,解构和指向类对象的机制

[英]C++ mechanism of constructing, deconstructing and pointer to class object

#include <iostream>
using namespace std;

class A
{
private:
   int m_i;
   friend int main(int argc, char const *argv[]);
public:
   A (int i = 0):m_i(i){};
   void display()
   {
       cout << m_i << endl;
   }
   int result() {return m_i;}
};

void createA(A *pa)
{
   pa = new A(1);
}

A* createA()
{
   A a(2);
   return &a;
}

void createAonstack()
{
   A a(3);
}

int main(int argc, char const *argv[])
{
   A a;
   A * pa = &a;
   pa->display();
   createA(pa);
   pa->display();

   A * a2 = createA();
   cout << a2->m_i << endl;
   createAonstack();
   cout << a2->m_i << endl;
   return 0;
}

The results of the program above is 上面的程序的结果是

0
0
2
3

How to explain the result 2 and 3? 如何解释结果2和3? From my understanding, the object created in function createA() should be deconstructed, and the pointer it returns should point to NULL , but why a2->m_i can be 2. And the 3 is even more confusing, as it seems that the function createAonstack() has nothing to do with a2. 根据我的理解,在函数createA()创建的对象应该被解构,并且它返回的指针应该指向NULL ,但是为什么a2->m_i可以是2.而且3更令人困惑,因为它似乎是函数createAonstack()与a2无关。

You said 你说

From my understanding, the object created in function createA() should be deconstructed, and the pointer it returns should point to NULL , but why a2->m_i can be 2 . 根据我的理解,在函数createA()创建的对象应该被解构,并且它返回的指针应该指向NULL ,但是为什么a2->m_i可以是2

It is true that 的确如此

the object created in function createA() should be deconstructed 应该解构在函数createA()创建的对象

It is not true that 这不是真的

and the pointer it returns should point to NULL 它返回的指针应指向NULL

The pointer returned by createA is non-NULL even though it is an invalid pointer to use in the calling function. createA返回的指针是非NULL,即使它是在调用函数中使用的无效指针。

but why a2->m_i can be 2 . 但为什么a2->m_i可以是2

It's pure coincidence. 这纯属巧合。 It really is undefined behavior. 它确实是未定义的行为。 Anything can happen when you dereference a2 . 当你取消引用a2时会发生任何事情。

Function createA() returns a pointer on a local variable, that is destroyed at quitting the function, so anything can happen, direct crash, or worse, the program will work as if everything is ok. 函数createA()返回一个局部变量的指针,该函数在退出函数时被销毁,因此任何事情都可能发生,直接崩溃,或者更糟糕的是,程序将像一切正常一样工作。

As Bjarne Stroustrup said: C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off. 正如Bjarne Stroustrup所说: C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off. C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.

As a quick tip, some more differentiable naming conventions might help readability in the future, as I personally find it a little hard to follow. 作为一个快速提示,一些更加可区分的命名约定可能有助于将来的可读性,因为我个人觉得它有点难以理解。

However, from what I can tell, it's a scoping problem. 但是,据我所知,这是一个范围问题。 CreateA() produces a local pointer, and when it leaves that function's scope, it's lost. CreateA()生成一个本地指针,当它离开该函数的范围时,它就会丢失。 So whatever you're actually accessing is essentially random. 所以你实际访问的内容基本上是随机的。

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

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