Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 构造函数和赋值运算符的行为_C++_Constructor_Assignment Operator - Fatal编程技术网

C++ 构造函数和赋值运算符的行为

C++ 构造函数和赋值运算符的行为,c++,constructor,assignment-operator,C++,Constructor,Assignment Operator,我已经把我的代码带到工作状态,但是还有几个问题。 因此,代码(其思想是在数组中可以存储一些数据和数据的子数组): 到目前为止,一切都很清楚。如果我取消对该行的注释 S(float d) {type = TP::INT; data = d;} S(int d) {type = TP::INT; data = d;} 然后我会收到同样的信息。但是,如果我取消注释该行 S(float d) {type = TP::INT; data = d;} S(int d) {type = TP::INT;

我已经把我的代码带到工作状态,但是还有几个问题。 因此,代码(其思想是在数组中可以存储一些数据和数据的子数组):

到目前为止,一切都很清楚。如果我取消对该行的注释

S(float d) {type = TP::INT; data = d;}
S(int d) {type = TP::INT; data = d;}
然后我会收到同样的信息。但是,如果我取消注释该行

S(float d) {type = TP::INT; data = d;}
S(int d) {type = TP::INT; data = d;}
我收到错误消息:

main.cpp:34: error: invalid conversion from 'Array<S>*' to 'int' [-fpermissive]
             M[SZ-1] = p;
             ~~~~~~~~^~~
         
但我不能,这会导致错误!但我可以删除数组类构造函数:

Array(Array const &a) {Resize(SZ+1); M[SZ-1] = a;}
这就行了。反过来说,如果我删除了结构构造函数,但保留了赋值操作符,那么就会出现错误

我有一些猜测,但我很困惑。有人能解释清楚为什么会有这种行为吗


如果我没有解释清楚,很抱歉。

摆脱所有隐式转换。他们是邪恶的。
int operator = (int d) {cout << "=int" << endl;type = TP::INT; return data = d;}
Array& operator = (T const &a) {
    Resize(SZ+1);
    M[SZ-1] = a;
    return *this;
}
Array(Array const &a) {Resize(SZ+1); M[SZ-1] = a;}