question1 赋值运算操作符
注意的问题书上讲的很详细了
下面是代码实现,但是VS有一个问题,strcpy安全性较低,虽然可以通脱编译,但是运行会报错,提示用strcpy_s()替代,但是,这里用strcpy()替代也不行,
//题目:如下为类型CMyString的声明,请为该类型添加赋值运算符
//以下为完整代码和测试用例
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
using namespace std;
class CMyString {
public:
CMyString(char* pData = nullptr);
CMyString(const CMyString& str);
~CMyString(void);
// operator "="
CMyString& operator =(const CMyString& str);
void print();//用来输出测试结果
private:
char*m_pData;
}; //the defination of the constructor fun
CMyString::CMyString(char* pData)
{
if (pData == nullptr)
m_pData = new char[];
m_pData[] = '\0';
} CMyString::CMyString(const CMyString& str)
{
int len = strlen(str.m_pData);
m_pData = new char[len + ];
//m_pData(str.m_pData);
strcpy(m_pData, str.m_pData); } CMyString::~CMyString()
{
//delete m_pData[];
delete[]m_pData;
} CMyString& CMyString:: operator =(const CMyString &str)
{
if (this == &str);
return *this; delete[]m_pData;
m_pData = nullptr; m_pData = new char[strlen(str.m_pData) + ];
strcpy(m_pData, str.m_pData);
return *this; } void CMyString:: print()
{
cout << m_pData << endl;
}
//测试用例 void test1()
{
char* str = "Hello World!";
CMyString str1(str);
CMyString str2;
str2 = str1;//call CMystring& operator=(const CMyString& str)
cout << " str is : " << str << endl;
//cout << "str2 is : " << str2.print() << endl;//没有重载<<运算符,还不能用
cout << "after 赋值" <<"str2 is : "<< endl;
str2.print();
} void test2()//赋值给自己
{
char* str = "Hello world!";
CMyString str1(str);
str1=str1;
cout << " str is : " << str << endl; cout << "after 赋值" << "str1 is : " << endl;
str1.print(); } void test3()//连续赋值
{
char* str = "Hello World!";
CMyString str1(str);
CMyString str2, str3;
str3 = str2 = str1;
cout << "str1 is : " << endl;
str1.print();
cout << "str2 is : " << endl; str2.print();
cout << "str3 is : " << endl;
str3.print();
} int main()
{
test1();
test2();
test3();
system("pause");
return ;
}
question1 赋值运算操作符的更多相关文章
- effective c++:对象的赋值运算
operator 中处理”自我赋值“ operator=操作符缺省情况下返回引用——TYPE& TYPE::operator=(const TYPE&),原因很简单,operator= ...
- 《Effective C++》第2章 构造/析构/赋值运算(2)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- 《Effective C++》第2章 构造/析构/赋值运算(1)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- PHP赋值运算
1. 赋值运算:= ,意思是右边表达式的值赋给左边的运算数. $int1=10; $int1=$int1-6; //$int1=4 echo $int1,"<br>"; ...
- js赋值运算的理解
简介 js引擎由于为了效率,很多时候的非直接量赋值都不是copy一份在赋值给新的变量,而是一个引用 ps:直接量:直接值数字字符串等 为什么使用len = doms.length; 里的len效率要比 ...
- 使用C/C++,赋值运算时发生的转换
使用C/C++,赋值运算时发生的转换主要有以下四种情况 一: 两边类型不同: 结果: 自动完成类型转换! 二: 长数赋给短数: 结果: 截取长数的低位送给短数! 三: 短数赋给长数: 结果: 原来是什 ...
- C++中的构造函数,拷贝构造函数和赋值运算
关于C++中的构造函数,拷贝构造函数和赋值运算,以前看过一篇<高质量C++/C编程指南>的文章中介绍的很清楚,网上能搜索到,如果想详细了解这方面的知识可以参看一下这篇文章. 常见的给对象赋 ...
- ecos3.0编译 if_lancepci.c:528: 错误: 赋值运算的左操作数必须是左值
/home/xin/ecos3/ecos-3.0/packages/devs/eth/amd/lancepci/v3_0/src/if_lancepci.c:528: 错误: 赋值运算的左操作数必须是 ...
- 算术运算,赋值运算,if语句,while,continue语句
算术运算 print(10 / 3)=3.333333333333 print(10 // 3)=3 print(10 ** 2)=100 赋值运算增量赋值 age=18 age+=1 print(a ...
随机推荐
- Mongodb 分片原理
1.主从mongodb 模式 类似,MySQL的主从配置 参照:https://blog.csdn.net/liusong0605/article/details/11551699 mongoDB有 ...
- python 切片技巧
说明: 字符串[开始索引:结束索引:步长] 开始索引:从指定位置开始截取: 结束索引:从指定位置结束截取,但不包含该位置的字符. 步长:不指定时步长为1: 1)当步长为正数时候,那么切片是从左到右进行 ...
- 安装完 Ubuntu 16.04.1,重启出现[sda] Assuming drive cache: write through的问题
重装了一下ubuntu,安装成功后重启出现了这个问题 刚开始以为是重启慢,就没在意这么多,可是我等了半个小时,(我特么的真闲,其实是忙别的忘了),还不行,咦,然后我就去找了找问题,哈哈哈哈 看图说话, ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:堆叠的水平
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(1)
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- Follow somebody
networkersdiary A personnel blog with Network Engineering articles https://networkersdiary.com/cisco ...
- Jquery 事件 文本框常用
1.只许输入类型 //只能输入整数和小数 function txtKeyUpDecimal(txtName) { getID(txtName).keyup(function(){ //keyup事件处 ...
- .hpp 文件
.hpp 是 Header Plus Plus 的简写,是 C++程序头文件. 其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp ...
- 操作系统OS,Python - 生产者消费者模型
1. 缓冲区(此处用阻塞队列充当),解决消费者和生产者强耦合问题.(生产者和消费者不直接通信) 2. 通过平衡生产者线程和消费者线程,来提高程序整体处理数据速度. 3. 在并发编程中该模式能解决大多数 ...
- 操作系统OS - 线程中的join()为什么叫join
1. 问题:很好奇为什么叫Join? 参考: https://blog.csdn.net/frankarmstrong/article/details/55504161 https://stackov ...