类中赋值函数注意事项
来源:互联网 发布:sql server使用 编辑:程序博客网 时间:2024/06/05 00:28
转载请标明是引用于 http://blog.csdn.net/chenyujing1234
欢迎大家拍砖
#include <stdlib.h>#include <stdio.h>class CTest{public:CTest() : m_value(100){}public:CTest & operator=(const CTest &cTestSource){printf("Enter CTest & operator=(const CTest &cTestSource)\n");m_value = cTestSource.m_value;return *this;}CTest * operator=(const CTest *cTestSource){printf("Enter CTest * operator=(const CTest *cTestSource)\n");m_value = cTestSource->m_value;return this;}public:int m_value;};int main(){CTest *pCTest = NULL;int *pInt = NULL;CTest cTest;// 这样pCTest一直为NULL//pCTest[0] = (CTest*)&cTest;//pCTest[1] = (CTest*)&cTest;// 这样pCTest可以得到地址值,但没有进入任何的赋值函数中pCTest = (CTest*)&cTest;// 没有报错,会进入函数CTest * operator=(const CTest *cTestSource)pCTest[1] = (CTest*)&cTest;// 会报错,会进入函数CTest & operator=(const CTest &cTestSource)// 报错地方:m_value = cTestSource.m_value;(这是因为此时pCTest[1]在内存中没有,所以访问值时出现报错)pCTest[1] = cTest;// pCTest[1]的地址为NULL,所以下面的调用是报错的 printf("m_value is %d\n", pCTest[1].m_value);system("pause");return 0;}
- 类中赋值函数注意事项
- 类中函数指针的赋值
- C++中类的赋值成员函数
- 类中赋值运算符重载函数
- 带指针成员类、析构函数出错、复制/赋值构造函数的注意事项
- Asp.net中数据类型赋值注意事项
- dlg类中函数写入的注意事项
- string类构造、拷贝构造、赋值、操作符函数实现及注意事项
- string类构造、拷贝构造、赋值、操作符函数实现及注意事项
- lua中函数注意事项
- 类 赋值函数
- java中关于拓展赋值运算符的注意事项!!
- mysql中count函数注意事项
- Linux中fgets函数注意事项
- Oracle 中常用函数注意事项
- String类中构造函数、拷贝构造函数、赋值函数的实现
- 类中构造函数、析构函数与赋值函数的重写
- 对象赋值注意事项
- 开启一个服务定时更新widget组件
- 2011工作总结
- AS 1.0--关于 ActionScript 1.0
- mysql innodb 刷数据和redo log
- Linux 命令 之 【chmod】 修改文件权限~
- 类中赋值函数注意事项
- 用MFC实现 个人pc监控系统
- AS 1.0--在 ActionScript 1.0 中创建一个自定义对象
- modelsim环境下仿真altera NCO IP核
- 2012-10-21-1
- 刘鹏主编《云计算》第二版全网首发高清PDF完整版
- SQL/Oracle取出第 m 条到第 n 条记录的方法
- Tomcat 修改端口号
- AS 1.0--在 ActionScript 1.0 中将方法分配给自定义对象