-6

I wanted to ask,

how does string::string operator function, I know that it is a standard constructor, for using strings, yet what does operator do? Does it allow me to use the multiplier operator at the end? Size_t represents the size of an object and string& is a pass by reference. How are these concepts making sense?

#include <iostream>
#include <string>
using namespace std::literals::string_literals;

std::string operator*(std::size_t n, const std::string& s)
{
   std::string ret;
   while (n--)
       ret += s;
   return ret;
}

int main()
{
std::cout << 5 * std::string("Hallo") << std::endl;
std::cout << 5 * "Test"s << std::endl;
}

What does std::string ret mean, can I use it because of std::string? Because std::string has been defined at the beginning ?

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
Dimitri
  • 31
  • 1
  • 10
  • 4
    There is *a lot* to unpack in this question. You could start by understanding what [operator overloading](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) is. Edit : `std::string ret;` is just a local variable. If that confuses you then you it's hard to know what a good starting point for you is, since it's hard to guess what you actually already known. Local variable declarations are a pretty fundamental concept. – François Andrieux Feb 04 '19 at 16:38
  • If you're asking about what overloading an operator does for a class, it allows you to define behavior for when that operator is applied to an instance of that class. In this case, `*` is overloaded to cause an `std::string` to contain its preexisting content repeatedly – Govind Parmar Feb 04 '19 at 16:38
  • And read a good C++ book. – Matthieu Brucher Feb 04 '19 at 16:39
  • I'm wondering if you are allowed to overload an operator that makes no mention of user provided types. – François Andrieux Feb 04 '19 at 16:41
  • where did you get this code from? Note that user supplied operators can do anything. Making the strings `operator*` performing a multiplication as it does in your code is just one choice and only the one who wrote the code can know what was the idea behind doing so – 463035818_is_not_an_ai Feb 04 '19 at 16:41
  • 4
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Feb 04 '19 at 16:43
  • @FrançoisAndrieux i am writing overloads for `ostream::operator<<` for things like eg `std::vector` all the time... – 463035818_is_not_an_ai Feb 04 '19 at 16:43
  • @FrançoisAndrieux `std::string` is a user provided type. If it isn't built in `int, char, double, ...` then it is user defined. That said, one should not overload operators for types they do not own. – NathanOliver Feb 04 '19 at 16:46
  • 2
    @user463035818 See [this talk](https://www.youtube.com/watch?v=BWvSSsKCiAw&t=1s) on why it can be a problem. Long story short, you open yourself up to ODR violations. – NathanOliver Feb 04 '19 at 16:47
  • @NathanOliver Could you reference a written discussion of this issue? I'm sure the talk is interesting, but I would rather get the gist in 5 minutes of reading... – Max Langhof Feb 04 '19 at 16:50
  • @NathanOliver I was more concerned with the restrictions on extending `std` but I guess it doesn't apply here. – François Andrieux Feb 04 '19 at 16:50
  • @MaxLanghof I don't have a source for a written discussion. What it boils down to is if you overload an operator for `std::string`, and then someone/something else in you code base provides their own overload for the same operator, then you now have an ODR violation that doesn't require any diagnostic and can be very difficult to track down. The talk is worth watching as he gets into a lot of other things you shouldn't do that a lot of people do. – NathanOliver Feb 04 '19 at 16:57
  • "how does string::string operator function" when people say the operator function, they usually mean the operator `()`; which std::string doesn't implement. – UKMonkey Feb 04 '19 at 18:53

1 Answers1

1

By implementing operator*, you allow type size_t to be "multiplied" by type string. The reason multiplied is in quotes is because you implement yourself what "multiply" means. In this particular implementation, the string is just appended to itself n times.

So 5 * std::string("Hallo") will result in HalloHalloHalloHalloHallo

Kon
  • 4,023
  • 4
  • 24
  • 38