Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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++有指针协方差,因此让你这样在腿上开枪:< /P> struct Base { Base() : a(5) {} int a; }; struct Child1 : public Base { Child1() : b(7) {} int b; int bar() { return b;} }; struct Child2 : public Base { Child2(): c(8) {} int c; }; int main() { Child1 children1[2]; Base * b = children1; Child2 child2; b[1] = child2; // <------- now the first element of Child1 array was assigned a value of type Child2 std::cout << children1[0].bar() << children1[1].bar(); // prints 57 } struct Base { Base():a(5){} INTA; }; 结构Child1:公共基 { Child1():b(7){} int b; int bar(){return b;} }; 结构Child2:公共基 { Child2():c(8){} INTC; }; int main() { 儿童1儿童1[2]; 基数*b=儿童1; 儿童2儿童2; b[1]=child2;//_C++_Pointers_Covariance - Fatal编程技术网

C++;指针协方差 我从来没有想到C++有指针协方差,因此让你这样在腿上开枪:< /P> struct Base { Base() : a(5) {} int a; }; struct Child1 : public Base { Child1() : b(7) {} int b; int bar() { return b;} }; struct Child2 : public Base { Child2(): c(8) {} int c; }; int main() { Child1 children1[2]; Base * b = children1; Child2 child2; b[1] = child2; // <------- now the first element of Child1 array was assigned a value of type Child2 std::cout << children1[0].bar() << children1[1].bar(); // prints 57 } struct Base { Base():a(5){} INTA; }; 结构Child1:公共基 { Child1():b(7){} int b; int bar(){return b;} }; 结构Child2:公共基 { Child2():c(8){} INTC; }; int main() { 儿童1儿童1[2]; 基数*b=儿童1; 儿童2儿童2; b[1]=child2;//

C++;指针协方差 我从来没有想到C++有指针协方差,因此让你这样在腿上开枪:< /P> struct Base { Base() : a(5) {} int a; }; struct Child1 : public Base { Child1() : b(7) {} int b; int bar() { return b;} }; struct Child2 : public Base { Child2(): c(8) {} int c; }; int main() { Child1 children1[2]; Base * b = children1; Child2 child2; b[1] = child2; // <------- now the first element of Child1 array was assigned a value of type Child2 std::cout << children1[0].bar() << children1[1].bar(); // prints 57 } struct Base { Base():a(5){} INTA; }; 结构Child1:公共基 { Child1():b(7){} int b; int bar(){return b;} }; 结构Child2:公共基 { Child2():c(8){} INTC; }; int main() { 儿童1儿童1[2]; 基数*b=儿童1; 儿童2儿童2; b[1]=child2;//,c++,pointers,covariance,C++,Pointers,Covariance,是的,这是未定义的行为 和No,一个典型的C++编译器,在这里,不太可能能够识别出一个值得诊断的东西,但是C++编译器一年比一年更聪明。谁知道将来的状态是什么? 然而,一个小小的诡辩: b[1] = child2; // <------- now the first element of Child1 array was assigned... children1衰变为Child1*。如果这就是事件结束的地方,你可以说b将是指向两元素数组的指针 但事情并没有就此结束。衰减的指针被转换为B

是的,这是未定义的行为

和No,一个典型的C++编译器,在这里,不太可能能够识别出一个值得诊断的东西,但是C++编译器一年比一年更聪明。谁知道将来的状态是什么? 然而,一个小小的诡辩:

b[1] = child2; // <------- now the first element of Child1 array was assigned...
children1
衰变为
Child1*
。如果这就是事件结束的地方,你可以说
b
将是指向两元素数组的指针


但事情并没有就此结束。衰减的指针被转换为
Base*
。您可以隐式地将指向子类的指针转换为指向超类的指针。但是(现在不严格地说)不能将指向子类数组的指针强制转换为超类数组。因此,
b
严格来说是指向单个元素的指针,
b[1]
成为未定义的行为。

我习惯于调用b[0]零元素。实际上创建数组是不必要的。我可以生成一个Child1 Child1;Base*b=&Child1;*b=child2;并获得相同的未定义行为,不是吗?不,这将是完美定义的行为。@Amomum:当然,仅仅因为它定义良好并不意味着这是一个好主意。也不意味着它能实现您所不具备的功能是的。@Amomum
b[0]
是元素零,但也是第一个元素。不,该片是定义的:它只执行一个
操作符=(Base&)
。这不太可能做任何有用的事情,但它是定义的。
Base * b = children1;