C++ - 在类上使用 &String[0] 访问字符*

C++ - Accessing char* with &String[0] on class

本文关键字:访问 字符 String C++      更新时间:2023-10-16

我只是在尝试和制作我自己的字符串类。(主要是因为有一些东西,我想建立在自定义方法,如"toBase64"等。无论如何,我想知道当你使用&String[0]时,如何访问char*的私有成员。

我认为你可以使用操作符重载,但我目前只把它作为String[0]返回char*。(我知道&;是指针操作符)。

String.h

namespace CoffeeBeans
{
    class _declspec(dllexport) Coffee_String
    {
        char* String;
        int StringLength;
    public:
        Coffee_String();
        Coffee_String(LPCSTR CString);
        LPSTR operator[](int);
        ~Coffee_String();
    };
}

String.cpp

#include "stdafx.h"
#include "String.h"
#include <Windows.h>
CoffeeBeans::Coffee_String::Coffee_String() {
    this->String = nullptr;
    this->StringLength = 0;
}
CoffeeBeans::Coffee_String::~Coffee_String() {
    if (String != nullptr) {
        delete[] this->String;
        this->String = nullptr;
        this->StringLength = 0;
    }
}
CoffeeBeans::Coffee_String::Coffee_String(LPCSTR CString) {
    int StringLength = strlen(CString) + 1;
    this->String = new char[StringLength]();
    this->StringLength = StringLength - 1;
    memcpy_s(this->String, StringLength, CString, StringLength);
}
LPSTR CoffeeBeans::Coffee_String::operator[](int)
{
    return this->String;
}

Main.cpp

    case WM_CREATE:{
        CoffeeBeans::Coffee_String String("Test");
        //I want to be able to do
        //strcpy_s(&String[0], 3, "hi"); //Copy "hi" into the private variable char*String.
        //I know this isn't a practical use, I wanted quick example (I would really pass it to recv (WinSock2))
        MessageBeep(0);
    break;
}

您的operator[]返回错误的值。为了让&String[index]访问正确的内存地址,operator[]需要返回对指定索引处字符的引用,而不是像您目前所做的那样返回字符串指针本身。

如果您查看std::string::operator[]的实际声明,您将看到它返回std::string::reference(又名char &)或std::string::const_reference(又名const char &)(取决于它是在非const或const std::string对象上调用)。

试试这样写:

String.h

namespace CoffeeBeans
{
    class _declspec(dllexport) Coffee_String
    {
        char* String;
        int StringLength;
    public:
        Coffee_String();
        Coffee_String(const Coffee_String &src);
        Coffee_String(const char *src);
        ~Coffee_String();
        char& operator[](int index);
        const char& operator[](int index) const;
        Coffee_String& operator=(const Coffee_String &rhs);
    };
};

String.cpp

#include "stdafx.h"
#include "String.h"
#include <algorithm>
#include <cstring> 
CoffeeBeans::Coffee_String::Coffee_String() {
    String = nullptr;
    StringLength = 0;
}
CoffeeBeans::Coffee_String::Coffee_String(const CoffeeBeans::Coffee_String &src) {
    StringLength = src.StringLength;
    String = new char[StringLength+1];
    std::copy(src.String, src.String+StringLength, String);
    String[StringLength] = 0;
}
CoffeeBeans::Coffee_String::Coffee_String(const char *src) {
    StringLength = std::strlen(str);
    String = new char[StringLength+1];
    std::copy(src, src+StringLength, String);
    String[StringLength] = 0;
}
CoffeeBeans::Coffee_String::~Coffee_String() {
    delete[] String;
    String = nullptr;
    StringLength = 0;
}
char& CoffeeBeans::Coffee_String::operator[](int index)
{
    return String[index];
}
const char& CoffeeBeans::Coffee_String::operator[](int index) const
{
    return String[index];
}
CoffeeBeans::Coffee_String& CoffeeBeans::Coffee_String::operator=(const CoffeeBeans::Coffee_String &rhs);
{
    Coffee_String temp(rhs);
    std::swap(String, temp.String);
    std::swap(StringLength, temp.String);
    return *this;
}

Main.cpp

case WM_CREATE: {
    CoffeeBeans::Coffee_String String("Test");
    strcpy_s(&String[0], 3, "hi"); //Copy "hi" into the private variable char *String...
    // note that the content of String will become "hit", not "hi"
    // and StringLength will still be 4...
    MessageBeep(0);
    break;
}