鱼C论坛

 找回密码
 立即注册
查看: 3310|回复: 2

C++两个奇怪的析构,求解释!!!!

[复制链接]
发表于 2013-3-2 14:26:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# include <iostream>

# include <string>
using namespace std;
struct date
{
    int year;
    int month;
    int day;
};
struct Person
{
    string name;
    int age;
    bool gender;
    double salary;
    date birth;
    Person()
    { cout << "创建Person对象" << this << endl; }
    ~Person()
    { cout << "释放Person对象" << this << endl;}
};
class autoptr
{
    Person* p;
    public:
    autoptr(Person* p):p(p){}
    ~autoptr()
    {
    delete p;
    }
    //一下两个是运算符函数重载!

    Person* operator->()//要访问谁的成员就返回谁的地址
    {return p;}
    Person operator*()
    { return *p; }
};
int main()
{
cout << "==================" << endl;
autoptr a = new Person;
cout << "==================" << endl;
a->name = "pangxie";
a->birth.year = 1995; //输出为0,为什么设置无效??
cout << (*a).name << endl;//为什么要加上(*a), 而不是*a.name??
cout << a->birth.year << endl;
cout << "==================" << endl;
return 0;
}
/*输出结果为
==================
创建Person对象0x8e5d008   //这个我知道是从哪里来的
==================
释放Person对象0xbfdfcce0   //这个是从哪里来的呢?
furong
释放Person对象0xbfdfcd00 //这个是从哪里来的呢?
==================
0                                           //为什么这个设置year没有成功??
释放Person对象0x8e5d008
*/
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-3 09:48:36 | 显示全部楼层
  1. # include <string>
  2. #include <iostream>
  3. using namespace std;
  4. struct date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };
  10. struct Person
  11. {
  12.         string name;
  13.         int age;
  14.         bool gender;
  15.         double salary;
  16.         date birth;
  17.         Person()
  18.         { cout << "创建Person对象" << this << endl; }
  19.         ~Person()
  20.         { cout << "释放Person对象" << this << endl;}
  21. };
  22. class autoptr
  23. {
  24.         Person* p;
  25. public:
  26.         autoptr(Person* p):p(p){}
  27.         ~autoptr()
  28.         {
  29.                 delete p;
  30.     }
  31.         //一下两个是运算符函数重载!
  32.        
  33.     Person* operator->()//要访问谁的成员就返回谁的地址
  34.         {return p;}
  35.         //Person operator*()
  36.         Person& operator*()//返回Person是返回一个*p的临时副本,所有的操作都将对这个副本进行
  37.                 //所以返回值改为引用类型
  38.         { return *p; }
  39. };
  40. int main()
  41. {
  42.         cout << "==================" << endl;
  43.         autoptr a = new Person;
  44.         cout << "==================" << endl;
  45.         a->name = "pangxie";
  46.         a->birth.year = 1995; //输出为0,为什么设置无效??
  47.         cout << (*a).name << endl;//为什么要加上(*a), 而不是*a.name??
  48.         //因为不能重载运算符.
  49.         cout << a->birth.year << endl;
  50.         cout << "==================" << endl;
  51.         return 0;
  52. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-3-3 16:00:43 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-10 16:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表