在这里,我a在main()不创建该类的对象的情况下调用类的构造函数,并且它看起来在调用之后立即调用析构函数.这里到底发生了什么?根据我的理解,它正在发生,因为我没有用一些内存创建一个对象?如何在这里调用dtor?它是如何实现的?请分享您对此的看法.
#include<iostream>
using namespace std;
class a{
public:
a(){
cout<<"\nctor";
}
~a(){
cout<<"\ndtor";
}
};
int main(){
a(); //why the dtor is getting called before the scope ends?
cout<<"\nctor_called\n";
}
Run Code Online (Sandbox Code Playgroud)
o/p该计划:
ctor
dtor
ctor_called
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |