免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6000 | 回复: 5
打印 上一主题 下一主题

[C++] C++: 请问如何在类的普通成员函数中调用类构造函数吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-22 16:05 |只看该作者 |倒序浏览
小弟要在一个类中实现一个执行清空的成员函数。由于清空操作除了释放内存外,其余操作跟初始化中的操作相同,因此我就在这个执行清空的成员函数中调用了构造函数,结果发现没有实现相应功能。用gdb跟踪调试,程序执行该成员函数过程中确实调用了构造函数,但构造函书调用前后,类内部数据成员未发生变化。

程序示意如下:


  1. //myclass.h
  2. class myclass
  3. {
  4. public:
  5.     myclass();
  6.     clear();

  7. private:
  8.     char * m_ptr;
  9.     int m_num;
  10. }
复制代码



  1. //myclass.cpp
  2. #include "myclass.h"

  3. myclass::myclass()
  4. {
  5.     m_ptr = 0;
  6.     m_num = 0;
  7. }

  8. myclass::clear()
  9. {
  10.     if(m_ptr) delete [] m_ptr;
  11.     myclass();
  12. }
复制代码

当在其他程序的myclass对象中执行clear()操作时,用gdb跟踪,发现程序执行了myclass()函数,但执行完后,m_num并不为0!
当用下述myclass.cpp文件替换上面的文件后,clear()功能正常。


  1. //myclass.cpp
  2. #include "myclass.h"

  3. myclass::myclass()
  4. {
  5.     m_ptr = 0;
  6.     m_num = 0;
  7. }

  8. myclass::clear()
  9. {
  10.     if(m_ptr) delete [] m_ptr;
  11.     m_ptr = 0;
  12.     m_num = 0;
  13. }
复制代码


这个问题我估计可能涉及到编译器对构造函数的调用,所以请大虾给小弟指点一下问题所在。

[ 本帖最后由 brooks_shenzhen 于 2007-5-22 16:08 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-05-22 17:22 |只看该作者
原帖由 brooks_shenzhen 于 2007-5-22 16:05 发表

  1. //myclass.cpp
  2. #include "myclass.h"

  3. myclass::myclass()
  4. {
  5.     m_ptr = 0;
  6.     m_num = 0;
  7. }

  8. myclass::clear()
  9. {
  10.     if(m_ptr) delete [] m_ptr;
  11.     myclass();
  12. }
复制代码


myclass();的调用只是又生成了另外一个类的实例,与调用该构造函数的实例不同.

论坛徽章:
0
3 [报告]
发表于 2007-05-22 17:41 |只看该作者
再者你在myclass()中連new都沒有,那來的delete []啊

论坛徽章:
0
4 [报告]
发表于 2007-05-22 17:44 |只看该作者
>> myclass();
这个调用只是生成了一个临时的类对象罢了,出了它所在的空间就被析构了.

另外,我猜想LZ的想法是调用构造函数重新构造该对象一次吧?这是不行的,构造函数只在对象生成的时候调用一次.

论坛徽章:
0
5 [报告]
发表于 2007-05-22 23:34 |只看该作者

  1. myclass::clear()
  2. {
  3.     if(m_ptr) delete [] m_ptr;
  4.     myclass();
  5. }

  6. 中的 myclass(); 仅仅是构造了一个myclass类的临时对象罢了。
复制代码


想要对分配好的内存用类构造函数初始化它可以用定位new操作符:


  1. // 好像是这个头文件
  2. #include <new>

  3. ...
  4. void myclass::clear()
  5. {
  6.     if (m_ptr)
  7.         delete[] m_ptr;

  8.     new(this) myclass;
  9. }
复制代码

不过我觉得还是不要期待用构造函数去复位类成员,也许这样更好些:

  1. class myclass {
  2.     public:
  3.         myclass()
  4.             : m_ptr(NULL), m_num(0)
  5.         {
  6.         }

  7.         void clear()
  8.         {
  9.             delete[] m_ptr;
  10.             reset();
  11.         }
  12.     private:
  13.         void reset()
  14.         {
  15.             m_ptr = NULL;
  16.             m_num = 0;
  17.         }

  18.         ....
  19. };
复制代码


我还是觉得将 delete[] ptr 跟 ptr = NULL 分开写不好,容易遗漏


[ 本帖最后由 jaffaz 于 2007-5-22 23:36 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-05-23 10:26 |只看该作者
感觉3楼的说的蛮对!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP