I think you wanted to implement operator++ for your class, and that should be implemented as:
Counter & operator++()
{
++count;
return *this;
}
Now the question is what does it do? It does pre-increment. Now you can write ++counter and that will invoke the above operator overload, and which internally will increment the variable count by 1.
Example :
Counter counter(1);
++counter;
std::cout << counter.get_count() << std::endl;
++(++counter);
std::cout << counter.get_count() << std::endl;
Output:
2
4
What your original code does?
If you try to run the above code using your original implementation of operator++, it will print the following :
2
3
That is because you're creating another temporary object which you're returning, which when you write ++(++counter) the outer pre-increment will increment the temporary. So the outer pre-increment will not change the value of counter.count.
Even if you write ++(++(++(++counter))), its equivalent to just ++counter.
Compare the output here:
Note ++(++counter) does NOT invoke undefined behavior.