c++中的new的应用

代码如下:

  1 #include <cstddef>
  2 #include <iostream>
  3 
  4 using namespace std;
  5 
  6 class CTest{
  7     public:
  8         int a = 12;
  9         void outVar(){
 10             cout << "var a: " << a;
 11         }
 12 };
 13 
 14 class CBest{
 15     public:
 16         int b = 90;
 17         void outVar(){
 18             cout << "var b: " << b;
 19         }
 20 };
 21 
 22 CTest *fun(CBest* pBest){
 23     CTest* pTest = new CTest();
 24     pTest->a = pBest->b;
 25     return pTest;
 26 }
 27 
 28 int main(int argc, char **argv)
 29 {
 30     CBest* pBest = new CBest();
 31     pBest->outVar();
 32     CTest* pRes = fun(pBest);
 33     pRes->outVar();
 34     if(pBest != NULL){
 35         delete pBest;
 36     }
 37     if(pRes != NULL){
 38         delete pRes;
 39     }
 40 
 41     return 0;
 42 }  

猜你喜欢

转载自www.cnblogs.com/guochaoxxl/p/11824090.html