I wonder if the following up-casting is valid? And if yes, is it possible to down-cast in the same way?
我想知道下面的上投是否有效?如果是,是否有可能以同样的方式向下预测?
class A {
public:
A(){ cout << "A constructor\n";};
A(const A& a){ cout << "A copy constructor\n"; };
};
class B : public A {
public:
B(){ cout << "B constructor\n";};
B(const B& b){ cout << "B copy constructor\n"; };
};
int main(){
B* b = new B;
cout << "--------\n";
A& a = *b;
return 0;
}
I got no output from doing A& a = *b;
, does this mean that doing this is equivalent to up-casting?
我做A&A=*b;没有得到输出,这是不是意味着这样做等同于向上投射?
更多回答
There are no casts in this code. A cast is something you write in your source code to tell the compiler to do a conversion.
此代码中没有强制转换。强制转换是您在源代码中编写的用来告诉编译器进行转换的东西。
You should be able to test this theory yourself, simply by compiling it. What happened when you tried it?
你应该能够自己测试这个理论,只需编译它即可。当你尝试的时候发生了什么?
a reference is a non-nullable const pointer, whenever a pointer would work a reference would work too, except for a nullpointer, there is no null-reference.
引用是不可为空的常量指针,只要指针起作用,引用也会起作用,除了空指针,没有空引用。
@AhmedWaleed There is only one default constructor called in your code.
@AhmedWaleed在您的代码中只调用了一个默认构造函数。
@SamVarshavchik I tried to write an explicit constructor and copy constructor with couts in body to test whether either is called, but i got empty console.
@SamVarshavchik我尝试编写一个显式构造函数,并在Body中使用Couts复制构造函数,以测试是否调用了这两个函数中的任何一个,但得到的控制台为空。
A& a = *b;
behaves exactly in the same way that A* a = b;
would, i.e. a
will be a reference bound to the A
base class subobject of the B
object or a pointer to the same subobject, respectively. It technically does however not involve any conversion (which is what you erroneously call a "cast"), instead it is just reference initialization.
A&a=*b;的行为方式与A*a=b;完全相同,即a将分别是绑定到B对象的A基类子对象的引用或指向相同子对象的指针。然而,从技术上讲,它不涉及任何转换(您错误地将其称为“强制转换”),而只是引用初始化。
But neither of these initializations ever have any side effects, nor could they ever involve creation of a new object, a constructor call or any other function call.
但是这些初始化都不会有任何副作用,也不会涉及创建新对象、构造函数调用或任何其他函数调用。
Downcasts are possible in the exact same way as well and also have no side effects:
下载也可以以完全相同的方式进行,而且也没有副作用:
B* b2 = static_cast<B*>(a);
if a
is a A*
or
如果a是A*或
B& b = static_cast<B&>(a);
if a
is a A&
. (Of course both have undefined behavior if the A
object referred to by the right-hand side isn't actually a base class subobject of an B
object.)
如果a是A&。(当然,如果右侧引用的A对象实际上不是B对象的基类子对象,则两者都具有未定义的行为。)
更多回答
"It technically does however not involve any implicit conversion" -- not completely true -- it does an implicit conversion of B&
to A&
, which can never have side effects (and in this case doesn't even change the pointer, as A
is the first and only base class of B
)
“然而,从技术上讲,它不涉及任何隐式转换”--并非完全正确--它实现了从B&到A&的隐式转换,这永远不会有副作用(在这种情况下,甚至不会改变指针,因为A是B的第一个也是唯一的基类)。
@ChrisDodd No, that's precisely the technicality I mean. This isn't an implicit conversion because it is none of the conversions listed in eel.is/c++draft/conv. Instead it is behavior that is explicitly specified for reference initialization, e.g. in eel.is/c++draft/dcl.init.ref#5.1.sentence-1: "(or, in either case, to the appropriate base class subobject of the object)".
@克里斯多德不,这正是我所说的技术细节。这不是隐式转换,因为它不是eel.is/c++Draft/conv中列出的转换。相反,它是为引用初始化明确指定的行为,例如在eel.is/c++Draft/dcl.init.ref#5.1.语句-1中:“(或者,在任何一种情况下,到对象的适当基类子对象)”。
@ChrisDodd Or at least it isn't a conversion in the sense of individual standard conversions or user-defined conversions. The use of the term "implicit conversion" is a bit unclear. So I removed it from my answer.
@ChrisDodd,或者至少它不是个别标准转换或用户定义转换意义上的转换。“隐式转换”一词的用法有些含糊。所以我把它从我的答案中删除了。
Its not a standard conversion -- it is a reference binding conversion, as defined in eel.is/c++draft/over.ics.ref. But yes, the precise terminology of 'conversion' vs 'impllicit conversion' vs 'standard conversion' vs 'cast' is unclear.
它不是标准转换--它是引用绑定转换,如eel.is/c++Draft/over.ics.ref中所定义。但的确,‘转换’和‘隐含转换’、‘标准转换’和‘CAST’的确切术语并不清楚。
The only definition I can find for implicit conversion is in eel.is/c++draft/over.best.ics, which contradicts itself (first saying that it is a standard conversion, then saying it is "governed by the rules for initialization of an object or reference by a single expression")
我能找到的隐式转换的唯一定义是在eel.is/c++Draft/over.Best.ics中,它自相矛盾(首先说这是一个标准转换,然后说它“受单个表达式的对象或引用的初始化规则的约束”)
我是一名优秀的程序员,十分优秀!