简体   繁体   English

Char赋值的重载运算符[] - C ++

[英]Overload operator[] for Char assignment - C++

I am fairly new to C++, although I do have some experience programming. 我是C ++的新手,虽然我有一些编程经验。 I have built a Text class that uses a dynamic char* as it's main member. 我已经构建了一个Text类,它使用动态char *作为它的主要成员。 The class definition is below. 类定义如下。

#include <iostream>
#include <cstring>
using namespace std;


class Text
{
  public:

    Text();
    Text(const char*); // Type cast char* to Text obj
    Text(const Text&); // Copy constructor
    ~Text();

    // Overloaded operators
    Text& operator=(const Text&);
    Text operator+(const Text&) const; // Concat
    bool operator==(const Text&) const;
    char operator[](const size_t&) const; // Retrieve char at
    friend ostream& operator<<(ostream&, const Text&);

    void get_input(istream&); // User input

  private:
    int length;
    char* str;
};

The issue I am having is I don't know how to use operator[] to assign a char value at the given index that's passed in. The current overloaded operator operator[] is being used to return the char at the index supplied. 我遇到的问题是我不知道如何使用operator[]在传入的给定索引处分配 char值。 当前重载的operator operator[]用于返回所提供索引的char Anyone have experience with this? 有人有这方面的经验吗?

I would like to be able to do something similar to: 我希望能够做类似的事情:

int main()
{
  Text example = "Batman";
  example[2] = 'd';

  cout << example << endl;
  return 0;
}

Any help and/or advice is appreciated! 任何帮助和/或建议表示赞赏!

Solution provided - Thanks a bunch for all the replies 提供解决方案 - 感谢所有回复

char& operator[](size_t&); works 作品

You need to provide a reference to the character. 您需要提供对角色的引用。

#include <iostream>

struct Foo {
   char m_array[64];
   char& operator[](size_t index) { return m_array[index]; }
   char operator[](size_t index) const { return m_array[index]; }
};

int main() {
    Foo foo;
    foo[0] = 'H';
    foo[1] = 'i';
    foo[2] = 0;
    std::cout << foo[0] << ", " << foo.m_array << '\n';
    return 0;
}

http://ideone.com/srBurV http://ideone.com/srBurV

Note that size_t is unsigned, because negative indexes are never good. 请注意, size_t是无符号的,因为负索引永远不会好。

您应该将operator[]重载为非const方法并从中返回引用

char& operator[](const int&);

This article is the definitive guide to operator overloading in C++ (which, to be honest, is mainly boilerplate code for syntactic sugar). 本文是C ++中运算符重载的权威指南(说实话,主要是语法糖的样板代码)。 It explains everything that is possible: Operator overloading 它解释了可能的一切: 运算符重载

Here's the portion that is of interest to you: 以下是您感兴趣的部分:

class X {
        value_type& operator[](index_type idx);
  const value_type& operator[](index_type idx) const;
  // ...
};

And yes, this is possible, for the many of the STL containers (the vector for example), allow for array subscript notation to access data. 是的,这是可能的,对于许多STL容器(例如vector ),允许数组下标符号访问数据。

So you can do something along the lines of this: 所以你可以按照以下方式做点什么:

char & operator[]( size_t i )
{
    return *(str + i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM