1

I'm learning C++ and this week I try to understand the operator overloading, no problems so far. Now I reached overloading the index operator and I managed it as follows,

std::string &operator[](size_t index);
std::string const &operator[](size_t index) const;

However, in several threads I read it is better to use a Proxy class, so I tried to implement a Proxy class and here the trouble starts. Note that I just implement the Proxy class in this simple class to understand how a Proxy class is working. I have already overloaded all functions without Proxy class and think it is time for the next step.

I have a class Strings which contains a pointer to several string objects. Suppose we have a object of the class Strings, called str, then I want to be able to use str[i] to get the i-th string and str[i][j] to get the j-th letter of the i-th string. Therefore I implemented a operator overloading in the Proxy class for [] and for <<, these are working fine.

Next, I want to be able to say str[i] = str[j], and therefore I implemented the operator=, however I am not able to get this right. Do you see what I'm doing wrong? There is no compile error, but the value isn't updated after calling str[i] = str[j].

And a subquestion, is the Proxy class implemented correct? It is working but I would also like to learn to use the language in a correct way.

Just to avoid confusion: I understand that this is much easier with a vector and that a Proxy class is not needed here. However, I want to understand how a Proxy class works and in my point of view it is best to start implementing it in a very simple class.

class Strings
{
  std::string *d_str;

  public:
    Strings();
    Strings(int argc, char *argv[]);

    // Some other functions
    // ...
    // End other functions

    class Proxy {
      std::string d_string;
      public:
        Proxy(std::string &str): d_string(str) {}

        char &operator[](int j) { return d_string[j]; }

        friend std::ostream &operator<<(std::ostream &out, Proxy const &rhs)
        {
          out << rhs.d_string << '\n';
          return out;
        }

        Proxy &operator=(Proxy const &other)
        {
          d_string = other.d_string;
          return *this;
        }
    };

    Proxy operator[](size_t idx)
    {
      return Proxy(d_str[idx]); 
    }

  private:
    //More functions here
};
  • IMHO, your proxy is unnecessary overhead. The only thing it offers over `Strings::operator[]` returning `d_str[idx]` directly is `Proxy::operator<<` adding an extra `\n` to the output, which you might not always want when outputting a single `string` from a `Strings` object. If you think you need a proxy, explain why. – Remy Lebeau Nov 24 '14 at 23:17
  • Use a `vector` instead.. Or return a reference to the pointed to string. – Brandon Nov 24 '14 at 23:18
  • I know that a vector is much easier and even better. However I'm just learning C++ and therefore I make a class with all possible overloading operators (and much more), just to practise with a simple class. – Michiel uit het Broek Nov 24 '14 at 23:21
  • 2
    Why do you need a proxy though? This seems to work: http://ideone.com/X5UEg6 the same as: http://ideone.com/WdnEOw – Brandon Nov 24 '14 at 23:29
  • I understand that a Proxy is not really necessary (or even not preferred) in this case. However, I just want to understand how the Proxy class works and choose to implement it in a very simple class. I already managed to overload all operators without Proxy class. – Michiel uit het Broek Nov 24 '14 at 23:34
  • It will never make sense in your case but here: http://stackoverflow.com/questions/6969881/operator-overload – Brandon Nov 24 '14 at 23:49

0 Answers0