方法一:

使用智能指针unique_ptr<>、shared_ptr<>进行自动管理。

方法二:

在局部环境下使用局部变量对其进行解引用,在函数结束的时候自动析构。

 1 #include <iostream>
 2 #include <string>
 3 
 4 class A
 5 {
 6 private:
 7         std::string s;
 8 public:
 9         A(std::string s) { this->s = s; }
10         ~A() { std::cout << this->s << "~A()" << std::endl; }
11 };
12 
13 int main()
14 {
15         A *a = new A("A");
16         A b("B");
17         A *c = new A("C");
18         A tc = *c;
19         return 0;
20 }

 

posted on 2018-05-05 20:34  RexfieldVon  阅读(215)  评论(0编辑  收藏  举报