*需要的效果:
()调用符号的重载实现实现
类似 MyString()(“hello world”)
将char的字符常量包装为MyString类型
类似java中的包装。**
.h文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyString {
//友元
friend ostream& operator<<(ostream& cout, MyString& str);
friend istream& operator>>(istream& cin, MyString& str);
public:
//无参构造
MyString();
//有参构造
MyString(const char* str);
//拷贝构造
MyString(const MyString& str);
//析构
~MyString();
//重载等号=
MyString& operator=(const MyString& str);
MyString& operator=(const char* str);
//重载[]
char& operator[](int index);
//重载+ 返回新的,所以返回值(复制品)
MyString operator+(const MyString& str);
MyString operator+(const char* str);
//重载==
bool operator==(const MyString& str);
bool operator==(const char* str);
//重载!=
bool operator!=(const MyString& str);
bool operator!=(const char* str);
//重载() 有参构造-》将string转化为MyString类型
MyString operator()(const char* str);
private:
//维护在堆区开辟的字符数组
char* pString;
//字符串长度
int size;
};
.myString 文件:
#include "myString.h"
//无参构造
MyString::MyString() {
//cout << "MyString 无参构造" << endl;
}
//有参构造
MyString::MyString(const char* str) {
//cout << "MyString 有参构造" << endl;
this->pString = new char[strlen(str) + 1];
strcpy(this->pString, str);
this->size = strlen(str);
}
//拷贝构造
MyString::MyString(const MyString& str) {
//cout << "MyString 拷贝构造" << endl;
this->pString = new char[strlen(str.pString)+1];
strcpy(this->pString, str.pString);
this->size = str.size;
}
//析构
MyString::~MyString() {
//cout << "MyString 析构" << endl;
if (this->pString != NULL) {
delete[] this->pString;
this->pString = NULL;
}
}
//重载<<
ostream& operator<<(ostream& cout, MyString& str) {
cout << str.pString;
return cout;
}
//重载>>
istream& operator>>(istream& cin, MyString& str) {
//清空原来堆区data
if (str.pString != NULL) {
delete[] str.pString;
str.pString = NULL;
}
//开辟临时缓冲区
char buf[1024];
cin >> buf;
str.pString = new char[strlen(buf) + 1];
strcpy(str.pString, buf);
str.size = strlen(buf);
return cin;
}
//重载= const MyString& str
MyString& MyString::operator=(const MyString& str) {
//cout << "重载= MyString operator= const MyString& str" << endl;
//清空原来堆区data
if (this->pString != NULL) {
delete[] this->pString;
this->pString = NULL;
}
this->pString = new char[str.size + 1];
strcpy(this->pString, str.pString);
this->size = str.size;
return *this;
}
//重载= const char* str
MyString& MyString::operator=(const char* str) {
//cout << "重载= MyString operator= const char* str" << endl;
//清空原来堆区data
if (this->pString != NULL) {
delete[] this->pString;
this->pString = NULL;
}
this->pString = new char[strlen(str) + 1];
strcpy(this->pString, str);
this->size = strlen(str);
return *this;
}
//重载[] 作为左值 要赋予& 来修改
char& MyString::operator[](int index) {
return this->pString[index];
}
//重载+ 返回新的,所以返回值(复制品)
MyString MyString::operator+(const MyString& str) {
//计算新str size,不释放空间
int newSize = this->size + str.size;
char* temp = new char[newSize + 1];
memset(temp, 0, newSize + 1);
strcat(temp, this->pString);
strcat(temp, str.pString);
//有参构造
MyString newString = temp;
delete[]temp;
return newString;
}
MyString MyString::operator+(const char* str) {
//计算新str size,不释放空间
int newSize = this->size + strlen(str);
char* temp = new char[newSize + 1];
memset(temp, 0, newSize + 1);
strcat(temp, this->pString);
strcat(temp, str);
//有参构造
MyString newString = temp;
delete[]temp;
return newString;
}
//重载==
bool MyString::operator==(const MyString& str) {
if (strcmp(this->pString, str.pString)) {
return true;
}
return false;
}
bool MyString::operator==(const char* str) {
if (strcmp(this->pString, str)) {
return true;
}
return false;
}
//重载!=
bool MyString::operator!=(const MyString& str) {
return !(strcmp(this->pString, str.pString));
}
bool MyString::operator!=(const char* str) {
return !(strcmp(this->pString, str));
}
//bug
//重载()仿函数 : 将string转化为MyString类型
MyString MyString::operator()(const char* str) {
//计算新str size,不释放空间
char* temp = new char[strlen(str) + 1];
strcpy(temp, str);
//有参构造
MyString newString = temp;
delete[]temp;
return newString;
}
测试文件 main入口
#include "myString.h"
void test01() {
//有参构造 -> MyString str = MyString("abc");同下
MyString str1 = "abc";
MyString str2 = MyString("abcd");
//拷贝构造 隐式 显式
MyString str3 = str2;
MyString str4 = MyString(str1);
//重载<<
cout << str1 << endl;
//重载>>
cin >> str1;
cout << str1 << endl;
cout << str2[2] << endl;
str2[2] = 'a';
cout <<"重载[]: " << str2 << endl;
}
void test02() {
MyString str1 = "hello";
MyString str2 = MyString("world");
//默认无参构造
MyString str3;
//赋值,不是拷贝构造
str3 = "java";
//赋值,不是拷贝构造
str2 = str3;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
MyString str4 = str3 + str1;
cout <<"重载+: " << str4 << endl;
MyString str5 = str3 + "hellowww";
cout <<"重载+: " << str5 << endl;
}
void test03() {
//重载== !=
MyString str1 = "hello";
MyString str2 = "hello";
MyString str3 = "world";
if (str1 == str2) {
cout << "str1==str2" << endl;
}
if (str1 == str3) {
cout << "str1==str3" << endl;
}
if (str1 == "hello") {
cout << "str1==hello" << endl;
}
if (str1 == "world") {
cout << "str1==world" << endl;
}
if (str1 != str2) {
cout << "str1!=str2" << endl;
}
if (str1 != str3) {
cout << "str1!=str3" << endl;
}
if (str1 != "hello") {
cout << "str1!=hello" << endl;
}
if (str1 != "world") {
cout << "str1!=world" << endl;
}
if (MyString("hello") == str1) {
cout << "()运算符重载,有参构造-》将string转化为MyString类型" << endl;
cout << "MyString(\"hello\") != str1" << endl;
}
}
//有问题 ()重载
void test04() {
MyString str1 = "hello";
MyString str2 = str1("hdddello");
MyString str3 = MyString()("heell");
cout << str1 << endl;
cout << str3 << endl;
cout << str2 << endl;
}
int main() {
//test01();
//test02();
//test03();
test04();
return 0;
}
你这样的话,仿函数和构造函数会产生歧义吧
hello
heell
hdddello
注意你输出顺序为str1
, str3
, str2
MyString(const char*)
就已经实现了将const char*字符串转换为MyString类型,没必要再写一个转换函数。