• 求教有关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”这个对象要析构两次?

  • 相关阅读:
    linux中apt-get使用
    部署ceph
    cinder存储服务
    ceph简介
    Horizon Web管理界面
    neutron网络服务2
    neutron网络服务
    nova计算服务
    cinder存储服务
    keystone身份认证服务
  • 原文地址:https://www.cnblogs.com/uestc/p/2103339.html
Copyright © 2020-2023  润新知