let us assume I have a class with
#include
using namespace std;
class Test{
public:
friend istream& operator >> (istr
You declared dummy const, so obviously mutating it during the lifetime of Test would break the contract about const.
This is what operator>> is doing at the moment and the compiler is helpfully trying to prevent you from breaking that contract.
operator>> actually do the initialization of Test?If operator>> should only do the initialization, not overwriting, of Test, then you should turn operator>> into a factory function, as demonstrated in gwiazdorrr's "recommended way".
If, on the other hand, operator>> should overwrite Test, then you're breaking the contract about the constness of dummy, and that's just bad. Therefore, dummy should be non-const.
dummy really need to be const?You could simply enforce the immutability of dummy through the interface of Test. Though in this case it could still be mutated inside the class, which is probably what you're trying to avoid.