Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload operator[] for Char assignment - C++

I am fairly new to C++, although I do have some experience programming. I have built a Text class that uses a dynamic char* as it's main member. 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. 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

like image 840
Joe Avatar asked Nov 30 '13 20:11

Joe


People also ask

Can [] operator be overloaded?

An overloaded operator (except for the function call operator) cannot have default arguments or an ellipsis in the argument list. You must declare the overloaded = , [] , () , and -> operators as nonstatic member functions to ensure that they receive lvalues as their first operands.

How do you write an assignment overloaded operator?

Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we'll get to. The assignment operator must be overloaded as a member function. This will call f1. operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves.

What is assignment overload?

Overload assignment is an assignment for extra pay in excess of the normal assignment of a full- time unit member.

How do you overload a char in C++?

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


2 Answers

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

Note that size_t is unsigned, because negative indexes are never good.

like image 71
kfsone Avatar answered Nov 15 '22 22:11

kfsone


This article is the definitive guide to operator overloading in C++ (which, to be honest, is mainly boilerplate code for syntactic sugar). 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.

So you can do something along the lines of this:

char & operator[]( size_t i )
{
    return *(str + i);
}
like image 25
turnt Avatar answered Nov 16 '22 00:11

turnt