I would appreciate a solution to the following problem: I have 2 types of variables
class Type1Str : public string
{
public:
Type1Str(const string & str) : string(str) {}
};
class Type2Str : public string
{
public:
Type2Str(const string & str) : string(str) {}
};
I would like to enjoy all the benefits of a string, but prevent cross assignments:
Type1Str t1;
Type2Str t2;
t1 = t2; // <= should not be allowed
Type2Str t2_1(t1); // <= should not be allowed
Type2Str t2_2("a string"); // <= should be allowed
How can I do this?
Unfortunately the OP presented code that didn’t compile.
Here’s the original code corrected in the most natural way so that it compiles:
Assuming that the above was what was meant, all that’s needed to prevent cross assignment is to make the converting constructor
explicit.In the line marked B the conversion that’s invoked is not an implicit conversion of the actual argument, but the
Type2Str‘s explicit conversion constructor. The actual argumentt1matches the formal argument of that constructor directly becauset2is astd::string. The OP wants to prevent also line B.One simple way is then to make the conversion even more explicit, namely, to name it.
I.e., to either introduce a carrier type for the formal argument, or to introduce an extra dummy conversion name argument, or to replace the public conversion constructor with a public factory function. The factory function is simplest and is as of 2012 cost free re efficiency, but it has a cost in maintenance: a derived class must reimplement it in order to offer that functionality. Of the other two solutions mentioned here, the extra dummy name argument is the easiest to implement and the least code:
So, what’s wrong with instead just declaring private constructors and assignment operators for all the the types that one wants to prohibit direct copying from?
Well, besides being brittle, for n types that amounts to O(n2) declarations. So it’s generally not a good idea. As I’m writing this, however, the OP has selected an answer with that approach as “the solution”. Just be warned: it’ really not a good idea, in general.