重写 [] C++

Overriding [] c++

我目前正在编写自己的字符串 class。 这是 header

#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();
};

这是class描述

#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".

谢谢:)

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

在你的字符串中 header:

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';
}

此外,对于字符串大小写,最好将这些方法内嵌在 header 文件中。还有一点,通常最好在此处放置一个断言以验证索引是否超出范围并且 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];
}