zoukankan      html  css  js  c++  java
  • 求教有关C++中子对象析构的问题

    在VS2010上,代码如下:

    View Code
    1 #include<iostream>
    2 #include<string>
    3  using namespace std;
    4
    5 class Person
    6 {
    7 public:
    8 Person(int age ,char* name );
    9
    10 void get() const;
    11
    12 ~Person()
    13 {
    14 cout<< "Destructor in the fun of Person! "<<endl;
    15 }
    16
    17
    18 private:
    19 int Age;
    20 std::string Name;
    21 };
    22 Person::Person(int age,char* name)
    23 {
    24 Age = age;
    25 Name =name;
    26 cout<<"\nConstructor in the fun of Person!"<<endl;
    27
    28 }
    29 void Person::get() const
    30 {
    31 cout<<" Age:"<<Age<<"\tName:"<<Name;
    32 }
    33
    34 class Wangmin: public Person
    35 {
    36 private:
    37 std::string University;
    38 std::string Address;
    39 char* NO;
    40 std::string Interest;
    41 int age;
    42 char* name;
    43 public:
    44 Wangmin(char* Uni,std::string Add,char* No,char* Intr,int Age,char* Name ):University(Uni),Address(Add),NO(No),Interest(Intr),Person(Age,Name)
    45 {
    46 cout<<"\nConstructor in the fun of Wangmin"<<endl;
    47 }
    48 void get(Wangmin& student) const
    49 {
    50 cout<<"University:"<< student.University<<"\t Address:"<<student.Address<< endl;
    51 cout<<"NO:"<<student.NO<<"\tInterest:"<<student.Interest<<endl;
    52 }
    53
    54 ~Wangmin()
    55 {
    56 cout<< "Destructor in the constructor fun of Wang_min"<<endl;
    57 }
    58
    59 };
    60
    61
    62
    63 int main()
    64 {
    65
    66
    67 Person Liu_Bei = Person(52,"liubei");
    68
    69 Liu_Bei.get();
    70
    71 Wangmin Wang_Min = Wangmin("TJPU","Hunan_Hengyang","201021010626","Telecomunication",25,"Wang_Min");
    72
    73 Wang_Min.get(Wang_Min);
    74
    75 }

    在VS2010上运行的结果,请问为什么“Wang_Min”这个对象要析构两次?

  • 相关阅读:
    python中的运算符的分类以及使用方法
    python的变量的命名规则以及定义
    C#和Java在重写上的区别
    IIS6 伪静态
    【读书笔记】Linux源码注释
    计算机是如何启动的?
    XSHELL下直接下载文件到本地(Windows)
    [转载]Linux 环境下编译 0.11版本内核 kernel
    虚拟化技术
    CentOS 6.4 编译安装LLVM3.3,Clang和Libc++
  • 原文地址:https://www.cnblogs.com/uestc/p/2103339.html
Copyright © 2011-2022 走看看