AllBull 2022-01-14 22:30 采纳率: 40%
浏览 21

一个简单的C++字符串的封装,重载() 仿函数时出现bug ,求解决

*需要的效果:
()调用符号的重载实现实现
类似 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;
}

  • 写回答

2条回答 默认 最新

  • [PE]经典八炮 2022-01-14 22:39
    关注

    你这样的话,仿函数和构造函数会产生歧义吧

    评论

报告相同问题?

问题事件

  • 修改了问题 1月14日
  • 修改了问题 1月14日
  • 修改了问题 1月14日
  • 创建了问题 1月14日

悬赏问题

  • ¥15 网络爬虫 在北京新发地抓取数据
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译
  • ¥15 STM32F103上电短路问题
  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。