string.h

#pragma once

class string
{
public:
string(const char* str = nullptr);
string(const string& str);
~string(); string& operator=(const string& str);
string& operator+=(const string& str);
char& operator[](int n) const;
char& operator[](int n);
bool operator==(const string& str) const; int size() const;
const char* c_str() const;
bool empty() const; friend const string operator+(const string& lhs, const string& rhs);
private:
char *data;
int length;
};

string.cpp

#include "string.h"
#include <string.h>
#include <stdio.h> const string operator+(const string& lhs, const string& rhs)
{
string tmp(lhs);
tmp += rhs;
return tmp;
} string::string(const char * str)
{
if (!str)
{
length = ;
data = new char[];
*data = '\0';
}
else
{
length = strlen(str);
data = new char[length + ];
strcpy(data, str);
}
printf("string constructor\n");
} string::string(const string & str)
{
length = str.size();
data = new char[length + ];
strcpy(data, str.c_str());
printf("string copy constructor\n");
} string::~string()
{
length = ;
delete[] data;
printf("string destructor\n");
} string& string::operator=(const string & str)
{
if (this == &str)
return *this;
delete[] data; length = str.size();
data = new char[length + ];
strcpy(data, str.c_str()); return *this;
} string & string::operator+=(const string & str)
{
length += str.size();
char *newdata = new char[length + ];
strcpy(newdata, data);
strcat(newdata, str.c_str());
delete[] data;
data = newdata;
return *this;
} char& string::operator[](int n) const
{
return data[n];
} char& string::operator[](int n)
{
return data[n];
} bool string::operator==(const string & str) const
{
if (length != str.size())
return false;
return strcmp(data, str.c_str()) ? false : true;
} int string::size() const
{
return length;
} const char * string::c_str() const
{
return data;
} bool string::empty() const
{
return length == ;
}

main.cpp

#include"string.h"

int main()
{
char a[] = "hello", b[] = "world";
string s1(a), s2(b);
string s3 = s1 + s2;
return ;
}

string的+运算符重载进行了返回值优化,在Visual Studio Release模式下main函数中会调用两次构造函数、一次复制构造函数、一次析构函数,比起不做优化减少了一次构造函数和一次析构函数

string constructor
string constructor
string copy constructor
string destructor
string destructor
string destructor

最新文章

  1. JAVA Thread线程异常监控
  2. 由Memcached升级到 Couchbase的 Java 客户端的过程记录(二)
  3. python_配置
  4. JS中this的值到底为何?
  5. POS管理系统之出入库单分页查询
  6. poj 3264
  7. PHP面向对象——异常处理
  8. php xls 导出乱码解决方案
  9. NSDictionary读取数据类型异常问题.
  10. [转]relative、absolute和float
  11. (2)jni编程学习笔记
  12. 【boost】BOOST_LOCAL_FUNCTION体验
  13. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
  14. HDU-1236 排名
  15. Rouh set 入门知识1(基础定义篇)
  16. Xcode7插件开发:从开发到拉到恶魔岛
  17. 我的CSDN之路
  18. Spark应用_PageView_UserView_HotChannel
  19. jQuery基础之二
  20. 20175202 《Java程序设计》第九周学习总结

热门文章

  1. Linux磁盘修复命令----fsck
  2. [Inno Setup] 执行程序,返回值不为0时提示用户
  3. Spring Boot JPA中java 8 的应用
  4. Web 之 Cookie
  5. POJ - 2387 Til the Cows Come Home (最短路入门)
  6. python selenium(键盘事件 Keys 类)
  7. Golang 实现 Redis(5): 用跳表实现SortedSet
  8. python执行脚本报错: Non-ASCII character &#39;\xe4&#39; in file yang.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
  9. GitHub上Asp.Net Core的源代码
  10. C# 中 枚举Enum 一些转换的方法整理