0

I have a class that somewhat imitates the lazy evaluation seen in shell variables. It saves the value as a string, but when operators like + are called, if both sides can be converted to int, it will add them, if both can't be converted to int, it treats it as a string concatenation, and if one can convert but the other can't, it prints an error message to handle the ambiguity.

The problem comes with the = operator. I've already written the member function, so that's not an issue. What I would like to have happen in addition to the member function, is that when the lhs is an string and the rhs is my class, the operator should return my class's value as is, but if the lhs is an integer, it should return std::stoi(Value).

I've been reading up on templates, conditions, and std::enable_if, so I think I'm on the right track, but I can't find any examples to wrap my head around how to accomplish this task.

In essence, what I want to do (although this code is illegal in C++) is the following:

MyClass operator = (std::string lhs, const MyClass rhs)
{
     return MyClass.Value;
}
MyClass operator = (int lhs, const MyClass rhs)
{
     return std::stoi(MyClass.Value);
}
  • Just to clarify: you want the *assignment operator* to **not store anything** and return a field of your class? – Botje Jun 08 '23 at 13:03
  • @Botje To be honest, I'm not sure how the assignment operator works as a non-member function, as it is my understanding that `lhs` is a local reference to the actual left-hand side, and therefore modifying `lhs` in a non-member setting won't actually change the left-hand side variable. So, I just wrote the return as a placeholder. – TheMohawkNinja Jun 08 '23 at 13:09
  • 1
    You want to define an `operator std::string` overload, to do this. – Sam Varshavchik Jun 08 '23 at 13:20
  • @SamVarshavchik .... and an `operator int` overload. – Peter Jun 08 '23 at 13:28
  • @SamVarshavchik Can you link me to an example of this, or show me what this would look like? I've been trying a few different interpretations of what you are stating, but it's not compiling. – TheMohawkNinja Jun 08 '23 at 14:21
  • `2 = something` ignores the `2` and returns the integer value of `something`? This seems completely bizarre. Use (explicit) conversion functions - see [here](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/16615725#16615725). – molbdnilo Jun 08 '23 at 14:57
  • See [the section on conversion operators, in Stackoverflow's guide to basic rules and idioms of operator overloading in C++](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/16615725#16615725) – Sam Varshavchik Jun 08 '23 at 15:52
  • @SamVarshavchik Thanks! – TheMohawkNinja Jun 08 '23 at 17:12
  • Why don't you store everything as a string and don't convert anything to `int`? Assignment would be trivial, it's just assigning a string. The plus operator would check whether both sides would be a well-formed integer, and perform a plus operation between strings if so. If they're not always strings, you might need to use `std::variant` or so to store these values. – Jan Schultke Jun 08 '23 at 20:19
  • @JanSchultke Even if I did it that way, when I go to assign some `int` to my class, wouldn't I still need to run the conversion function as suggested above? – TheMohawkNinja Jun 08 '23 at 20:49
  • You would need to use `std::to_string` when storing the value in a string. The only place where you need to worry about converting to `int` is when doing arithmetic like `+`. If this is for shell variables, then `int` doesn't make much sense anyways, because it arbitrarily restricts the maximum value, and typically you want `+` to work for any integer shell variables, no matter how large. So consider using some form of BigInt too. – Jan Schultke Jun 08 '23 at 21:21
  • 2
    You **can’t** implement `operator=` as a non-member function! – Davis Herring Jun 09 '23 at 00:19
  • To be clear, you understand that the **purpose of** `operator=` **is to modify** the left-hand side, according to the current value of the right-hand side? – Karl Knechtel Jun 09 '23 at 04:08
  • Does this answer your question? [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – Karl Knechtel Jun 09 '23 at 04:09

0 Answers0