W przypadku rzutowania ((A)b) jest wywoływany konstruktor kopiujący.
#include<cstdio>
class A {
public:
A() {
printf("constructor of A\n");
}
A(A const& a) {
printf("copy constructor\n");
}
virtual void method() {
printf("method A\n");
}
};
class B : public A {
public:
void method() {
printf("method B\n");
}
};
int main() {
B b;
b.method();
((A)b).method();
return 0;
}
/**
constructor of A
method B
copy constructor
method A
*/
W przypadku operacji na wskaźnikach, mielibysmy wynik:
/**
constructor of A
method B
method B
*/