Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator[]= overload?

Okay, I'm trying to make a quick little class to work as a sort of hash table. If I can get it to work then I should be able to do this:

  StringHash* hash = new StringHash;
  hash["test"] = "This is a test";
  printf(hash["test"]);

And it should print out "This is a test".

It looks like I have 2 problems at this point. Firstly I did this:

const char* operator[](const char* key) {
  for(int i = 0; i < hashSize; ++i) {
    if(strcmp(hkeys[i], key) == 0) {return values[i];}
  }
  return NULL;
}

But when I try to look up a value the compiler complains that

error: invalid types `StringHash*[const char[5]]' for array subscript

Secondly operator[]= does not appear to be the correct syntax here. The only other thing I could find was &operator[] but I don't think that will work since I have to code the lookup procedure??? (Isn't that syntax just used to return an array item reference?)

Is what I'm trying to do here even possible? Any advice appreciated. :)


Seems to be some confusion about what I'm trying to do. I'll post my code:

http://pastebin.com/5Na1Xvaz


Finished product after all the help:

http://pastebin.com/gx4gnYy8

like image 711
Khat Avatar asked Oct 11 '10 14:10

Khat


People also ask

How do I overload operator+?

The name of an overloaded operator is operator x, where x is the operator as it appears in the following table. For example, to overload the addition operator, you define a function called operator+. Similarly, to overload the addition/assignment operator, +=, define a function called operator+=.

Can we overload += operator?

Operator Overloading in C++ This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.

What do you mean by operator overloading?

Polymorphism: Polymorphism (or operator overloading) is a manner in which OO systems allow the same operator name or symbol to be used for multiple operations. That is, it allows the operator symbol or name to be bound to more than one implementation of the operator. A simple example of this is the “+” sign.

Can we overload == operator in C++?

Overloading Binary Operators Suppose that we wish to overload the binary operator == to compare two Point objects. We could do it as a member function or non-member function. To overload as a member function, the declaration is as follows: class Point { public: bool operator==(const Point & rhs) const; // p1.


3 Answers

The error is because hash is a pointer. Change to:

StringHash hash;
like image 144
Yakov Galka Avatar answered Oct 21 '22 03:10

Yakov Galka


The other answers relate to your first question. As for your second...

If you return a reference, then you're returning an lvalue. You can always assign to an lvalue.

Yes, it (pretty much) really is that simple. I recommend reading carefully for whether or not you need const in various places, though.

What I remember reading is that you should provide a const and a non-const overload for operator[], something like so:

MyType const &operator[](int index) const; // This is the array access version (no assignment allowed), which should work on const objects
MyType &operator[](int index);      // This is the array access or assignment version, which is necessarily non-const.

See this link for more information.

like image 42
Platinum Azure Avatar answered Oct 21 '22 03:10

Platinum Azure


hash isn't a StringHash object. Its a pointer to one.

You can do this:

(*hash)["test"] = "This is a test";

Or you can ask yourself why you need a pointer to it in the first place,

StringHash hash;
hash["test" = "This is a test";

... or even if you do, why you wouldn't use a smart pointer like auto_ptr.

#include <memory>
std::auto_ptr<StringHash> hash( new StringHash );
(*hash)["test"] = "This is a test";
like image 20
John Dibling Avatar answered Oct 21 '22 03:10

John Dibling