C++ - 在类上使用 &String[0] 访问字符*
C++ - Accessing char* with &String[0] on class
我只是在尝试和制作我自己的字符串类。(主要是因为有一些东西,我想建立在自定义方法,如"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 "hi t ", not "hi "
// and StringLength will still be 4...
MessageBeep(0);
break;
}
相关文章:
- 按字符值访问int数组
- 如何循环访问常量字符**?
- 固有构造函数的字符和访问级别
- 在函数 strcpy() 中访问字符数组时出现分段错误
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 为什么没有访问所有字符串字符?
- 将静态字符数组中的字符分配给动态分配的字符数组 - 访问冲突
- 当我用"ñ"字符循环访问字符串时出现奇怪的结果
- 循环访问 C++ 个字符数组的位
- 通过C++中的指针访问字符数组
- 访问字符阵列中不可用的内存位置(超出范围值)
- 访问字符* 的 3 元组值
- 通过指针访问字符*
- 访问字符c++中的各个位
- 两个两个地访问字符
- 如何使从基类继承的对象访问字符方法,但有自己的结果
- C++ - 在类上使用 &String[0] 访问字符*
- 访问字符指针
- 循环访问字符串会导致分段错误
- 访问字符指针