gpt4 book ai didi

c++ - 重写 [] C++

转载 作者:行者123 更新时间:2023-11-30 03:32:15 24 4
gpt4 key购买 nike

我目前正在编写自己的 String 类。这是标题

#pragma once

class String
{
private:
char* m_Beginning;
int m_Length;

public:
// Constructors
String();
String(const String&);
String(const char*);

// Destructor
~String();

// Operations
String& operator=(const String&);
String operator+(const String&)const;
String& operator+=(const String&);
char operator[](int _Index)const;

// Methods
void Append(const String&);
String Concatenate(const String&)const;
int Length()const { return m_Length; };
void Clear();
};

这是类的描述

#include "String.h"
#include <cstring>
#include <iostream>

String::String()
{
m_Beginning = nullptr;
m_Length = 0;
}

String::String(const String& _String)
{
m_Length = _String.Length();
m_Beginning = new char[m_Length];
for (int i = 0; i < m_Length; ++i)
{
m_Beginning[i] = _String[i];
}
}

String::String(const char* _String)
{
m_Length = strlen(_String);
m_Beginning = new char[m_Length];
for (int i = 0; i < m_Length; ++i)
{
m_Beginning[i] = _String[i];
}
}

String::~String()
{
delete[] m_Beginning;
}

String& String::operator=(const String& _String)
{
Clear();
m_Length = _String.Length();
m_Beginning = new char[m_Length];
for (int i = 0; i < m_Length; ++i)
{
m_Beginning[i] = _String[i];
}
return *this;
}

String String::operator+(const String& _String)const
{
String NewString(*this);
NewString += _String;
return NewString;
}

String& String::operator+=(const String& _String)
{
for (int i = 0; i < _String.Length(); ++i)
{
m_Beginning[m_Length + i] = _String[i];
}
m_Length += _String.Length();
return *this;
}

char String::operator[](int _Index)const
{
return m_Beginning[_Index];
}


void String::Append(const String& _String)
{
*this += _String;
}

String String::Concatenate(const String& _String) const
{
return (*this + _String);
}

void String::Clear()
{
delete[] m_Beginning;
m_Beginning = nullptr;
m_Length = 0;
}

我想问的是如何覆盖运算符 [] 以便我为某个单元格设置一个值,而不仅仅是提取它。

str("ABCD");
str[2] = 'Z'; // and str will become "ABZD".

谢谢你:)

最佳答案

您需要返回对字符串内部缓冲区中字符的引用。您还需要为您的字符串提供 operator[] 的 const 和非 const 版本。

在您的字符串标题中:

const char& String::operator[](int _Index) const;
char& String::operator[](int _Index);

并在您的 .cpp 文件中:

const char& String::operator[](int _Index) const
{
return m_Beginning[_Index];
}

char& String::operator[](int _Index)
{
return m_Beginning[_Index];
}

当您尝试使用如下代码在字符串中分配一个字符时:

str[2] = 'Z'; // and str will become "ABZD".

编译器将有效地生成与此代码相同的代码:

{
char& ch = str.m_Beginning[2];
ch = 'Z';
}

此外,对于字符串大小写,最好将这些方法内联到头文件中。还有一点,通常最好在此处放置一个断言以验证索引是否超出范围并且 m_Beginning 已分配:

const char& operator[](int _Index) const
{
assert(m_Beginning && _Index>=0 && _Index<m_Length);
return m_Beginning[_Index];
}

char& operator[](int _Index)
{
assert(m_Beginning && _Index>=0 && _Index<m_Length);
return m_Beginning[_Index];
}

关于c++ - 重写 [] C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43598689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com