我正在用 C++ 组合我自己的(愚蠢的)标量/列表/哈希(perl-like..)东西。
我遇到了必须将标量取消引用到列表中的问题,并且在尝试初始化时它不起作用。
List
几个默认构造函数,其中 5 个范围从List()
到List(Scalar, Scalar, Scalar, Scalar)
.
List stuff(1, 2, 3);
Scalar aref = stuff; // List overloads the (Scalar) cast operator
// the following line is the unwanted thing..
List failist = aref; // uses List::List(Scalar), so same as `List failist(aref);`
// however, these work
List thisworks;
thisworks = aref;
thisworks.operator=(aref);
列表标题:
class List : public Thing {
std::vector<Scalar> stuff;
public:
List();
List(Scalar s1); // this gets called because initialization
List(Scalar s1, Scalar s2);
List(Scalar s1, Scalar s2, Scalar s3);
List(Scalar s1, Scalar s2, Scalar s3, Scalar s4);
List &operator=(const Scalar &other); // but i want this
/* some getters/setters cut away */
operator Scalar();
};
我真的很想用List mylist = listreference;
,怎么办?