由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++: static_cast and dynamic_cast
相关主题
相关话题的讨论汇总
话题: dog话题: pet话题: print话题: virtual话题: cat
进入Programming版参与讨论
1 (共1页)
n**d
发帖数: 9764
1
Can you expain the output of the following code?
d1d = 0
Dog
d1s = 7275136
Dog
d2d = 7275136
Cat
d2s = 7275136
Cat
#include
using namespace std;
class Pet { public: virtual ~Pet(){}};
class Dog : public Pet {
public:
void print() { cout << "Dog" << endl; };
};
class Cat : public Pet {
public:
void print() { cout << "Cat" << endl; };
};
int main() {
Pet* b = new Cat; // Upcast
// Try to cast it to Dog*:
Dog* d1d = dynamic_cast(b);
Dog* d1s = static_cast
m********a
发帖数: 1312
2
//正常,因为dynamic cast会做type check,
d1d = 0
//侥幸没crash
Dog
//static cast不做type check
d1s = 7275136
//侥幸没crash
Dog
//正规合法的用法。
d2d = 7275136
Cat
//static cast也能通过
d2s = 7275136
Cat
n**d
发帖数: 9764
3
It runs on g++ 3.4.4 and g++ 2.95.3. No crash at all. Could any ppl run it with VC++?

【在 m********a 的大作中提到】
: //正常,因为dynamic cast会做type check,
: d1d = 0
: //侥幸没crash
: Dog
: //static cast不做type check
: d1s = 7275136
: //侥幸没crash
: Dog
: //正规合法的用法。
: d2d = 7275136

r*********r
发帖数: 3195
4
no crash because of static binding.
define "print" as virtual, and it will crash.
n**d
发帖数: 9764
5
It arises more questions. One virtual method (~pet()) is enough to enable
polymorphism. No matter print() is virtual or not, print() will be called
according to the caller's polymorphic class. Why do we have to define it as virtual? In
the other word, why do you say it is "static binding" with non-virtual print
()?

【在 r*********r 的大作中提到】
: no crash because of static binding.
: define "print" as virtual, and it will crash.

h*******e
发帖数: 225
6
seems you dont understand what is virtual. without virtual, the print method
has no dynamic type, so the compiler could easily optimize the call site to
produce a normal function call. and you didn't use anything related to '
this' pointer in the method. that's why it didn't crash. so this means even
if you dont use virtual, using any field in that class will still make it
crash.

In
print

【在 n**d 的大作中提到】
: It arises more questions. One virtual method (~pet()) is enough to enable
: polymorphism. No matter print() is virtual or not, print() will be called
: according to the caller's polymorphic class. Why do we have to define it as virtual? In
: the other word, why do you say it is "static binding" with non-virtual print
: ()?

t****t
发帖数: 6806
7
你对virtual的理解完全错了

In
print

【在 n**d 的大作中提到】
: It arises more questions. One virtual method (~pet()) is enough to enable
: polymorphism. No matter print() is virtual or not, print() will be called
: according to the caller's polymorphic class. Why do we have to define it as virtual? In
: the other word, why do you say it is "static binding" with non-virtual print
: ()?

n**d
发帖数: 9764
8
In this code, could we say Dog::print() overrides Pet::print()?
class Pet {
public:
void print() {cout << "Pet" << endl;}
virtual ~Pet(){}
};
class Dog : public Pet {
public:
void print() { cout << "Dog" << endl; };
};

【在 t****t 的大作中提到】
: 你对virtual的理解完全错了
:
: In
: print

h*******e
发帖数: 225
9
they are in two different namespaces...that's not 'override'

【在 n**d 的大作中提到】
: In this code, could we say Dog::print() overrides Pet::print()?
: class Pet {
: public:
: void print() {cout << "Pet" << endl;}
: virtual ~Pet(){}
: };
: class Dog : public Pet {
: public:
: void print() { cout << "Dog" << endl; };
: };

r*********r
发帖数: 3195
10
no virtual, no overriding.
m********a
发帖数: 1312
11
借用你的例子:
#include
using namespace std;
class Pet {
public:
virtual void print() {cout << "Pet" << endl;}
virtual ~Pet(){}
};
class Dog : public Pet {
public:
void print() { cout << "Dog" << endl; };
};
int main() {
Pet * p = new Dog();
p->print();
return 0;
}
This will print "Dog". And this is why print function needs to be virtual in
Pet class. Without the virtual declaration, it will print "Pet".
1 (共1页)
进入Programming版参与讨论
相关主题
相关话题的讨论汇总
话题: dog话题: pet话题: print话题: virtual话题: cat