zoukankan      html  css  js  c++  java
  • 实现不在栈中产生对象

     1 #include "stdafx.h"
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <iostream.h>
     5 
     6 class MyInt
     7 {
     8 private:
     9     int  m_nYear;
    10     int  m_nMonth;
    11     int  m_nDay;
    12 public:
    13 
    14   MyInt(int nYear = 0, int nMonth = 0, int nDay = 0)
    15   {
    16     m_nYear = nYear;
    17     m_nMonth = nMonth;
    18     m_nDay = nDay;
    19   }
    20 
    21   MyInt(const MyInt& theObj)
    22   {
    23     m_nYear = theObj.m_nYear;
    24     m_nMonth = theObj.m_nMonth;
    25     m_nDay = theObj.m_nDay;
    26   }
    27 
    28   ~MyInt()
    29   {
    30     m_nDay = 0;  
    31   }
    32 
    33 public:
    34 //   operator int()
    35 //   {
    36 //     return m_nYear;
    37 //   }
    38 };
    39 
    40 
    41 class MyTest
    42 {
    43 private:
    44   MyTest()
    45   {
    46     cout << "产生对象了, 亲!" << endl;
    47   }
    48   ~MyTest()
    49   {
    50     cout << "开始析构了, 亲!" << endl;
    51   }
    52 
    53 public:
    54   void Destructor()
    55   {
    56     delete this;
    57   }
    58   static MyTest* NewMyTest()
    59   {
    60     return (new MyTest);
    61   }
    62 };
    63 
    64 
    65 
    66 int main(int argc, char* argv[])
    67 {
    68  
    69   // MyTest theT;  //  不能在栈中产生对象
    70   MyTest* lpT = MyTest::NewMyTest(); // 在堆中产生对象
    71 
    72   
    73   //  自己调用析构
    74 
    75   lpT->Destructor();
    76 
    77   return 0;
    78 }
  • 相关阅读:
    C# Arrays
    C# 类 (12)
    C# 类 (11)
    C# 类 (10)
    常用的HDFS操作
    Java StringTokenizer 类使用方法
    常用HBase操作
    常用Linux命令
    彻底关闭Windows10的更新
    如何将百度坐标转换为国家2000(或WGS84)坐标系?
  • 原文地址:https://www.cnblogs.com/Fightingbirds/p/2798910.html
Copyright © 2011-2022 走看看