33,311
社区成员
发帖
与我相关
我的任务
分享
class B {
int x;
protected:
int y;
public:
int z;
B( int a = 0, int b = 0, int c = 0 ): x( a ), y( b ), z( c ) { }
void Setx( int a ) { x = a; }
void Sety( int a ) { y = a; }
int Getx() { return x; }
int Gety() { return y; }
void ShowB()
{ cout << "x = " << x << '\t' << "y = " << y << '\t' << "z = " << z << '\n'; }
};
class C: public B {
int xc;
public:
C( int a = 0, int b = 0, int c = 0, int d = 0 ): B( a, b, c ), xc( d ) { }
void Setxc( int a ) { xc = a; }
int Getxc() { return xc; }
void ShowC()
{ cout << "x = " << Getx() << '\t' << "y = " << y << '\t' << "z = " << z << '\t';
cout << "xc = " << xc << endl;
}
};
class D: public C {
int xd;
public:
D( int a = 0, int b = 0, int c = 0, int d = 0, int e = 0 ): C( a, b, c, d ), xd( e ) { }
void setxd( int a ) { xd = a; }
int Getxd() { return xd; }
void ShowD()
{ cout << "x = " << Getx() << '\t' << "y = " << y << '\t' << "z = " << z << '\t';
cout << "xc = " << Getxc() << '\t' << "xd = " << xd << endl;
}
};
int main()
{
B b, *pb;
C c, *pc;
D d, *pd;
pd = &d;
pb = static_cast< B* >( pd );
pc = static_cast< C* >( pd );
// pb -> ShowD(); //这个指针始终没有变啊!一直是指向B类的啊!
//你进行转换时也没有换成pd啊;
//
//你要想吧B 转换成D时就要用下面的语句。
pd=static_cast<D*>(pb);
pd->ShowD();
return 0;
}
class B
{
public:
virtual void show()
{
cout<<"B"<<endl;
}
};
class D:public B
{
public:
void show()
{
cout<<"D"<<endl;
}
};
int main()
{
B *b=new B;
D *d=new D;
b->show();
d->show();
delete b;
b=static_cast<B*>(d);
b->show();
delete b;
return 1;
}
/*
I'am sorry ! 我刚才放一个小错误!static_cast 向上转换是安全的。向下是不安全的。
我想歪了。但是
dynamic_cast也可以用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。
*/
//msdn examples
// static_cast_Operator.cpp
// compile with: /LD
class B {};
class D : public B {};
void f(B* pb, D* pd) {
D* pd2 = static_cast<D*>(pb); // not safe, pb may
// point to just B
B* pb2 = static_cast<B*>(pd); // safe conversion
}
////////////////////
// dynamic_cast_2.cpp
// compile with: /c /GR
class A {virtual void f();};
class B {virtual void f();};
void f() {
A* pa = new A;
B* pb = new B;
void* pv = dynamic_cast<void*>(pa);
// pv now points to an object of type A
pv = dynamic_cast<void*>(pb);
// pv now points to an object of type B
}
/*class B{...};
class C:public B{...};
class D:public C{...};
viod f(D *pd)
{
C *pc = dynamic_cast<C *>(pd);//正确:C是D的直接派生类
B *pb = dynamic_cast<B *>(pd);//正确:B是D的间接派生类
...
}*/
#include <iostream>
using namespace std;
class B {
int x;
protected:
int y;
public:
int z;
B( int a = 0, int b = 0, int c = 0 ): x( a ), y( b ), z( c ) { }
void Setx( int a ) { x = a; }
void Sety( int a ) { y = a; }
int Getx() { return x; }
int Gety() { return y; }
void ShowB()
{ cout << "x = " << x << '\t' << "y = " << y << '\t' << "z = " << z << '\n'; }
};
class C: public B {
int xc;
public:
C( int a = 0, int b = 0, int c = 0, int d = 0 ): B( a, b, c ), xc( d ) { }
void Setxc( int a ) { xc = a; }
int Getxc() { return xc; }
void ShowC()
{ cout << "x = " << Getx() << '\t' << "y = " << y << '\t' << "z = " << z << '\t';
cout << "xc = " << xc << endl;
}
};
class D: public C {
int xd;
public:
D( int a = 0, int b = 0, int c = 0, int d = 0, int e = 0 ): C( a, b, c, d ), xd( e ) { }
void setxd( int a ) { xd = a; }
int Getxd() { return xd; }
void ShowD()
{ cout << "x = " << Getx() << '\t' << "y = " << y << '\t' << "z = " << z << '\t';
cout << "xc = " << Getxc() << '\t' << "xd = " << xd << endl;
}
};
int main()
{
B b, *pb;
C c, *pc;
D d, *pd;
pd = &d;
pb = static_cast< B* >( pd );
pc = static_cast< C* >( pd );
pb -> ShowD(); //这个为什么不能调用了.地址还是
return 0;
}
#include <iostream>
#include <bitset>
using namespace std;
class B
{
public:
virtual void show()
{
cout<<"B"<<endl;
}
};
class D:public B
{
public:
void show()
{
cout<<"D"<<endl;
}
};
int main()
{
B *b=new B;
D *d=new D;
b->show();
d->show();
delete b;
b=dynamic_cast<B*>(d);
b->show();
delete b;
return 1;
}
/*
dynamic_cast 用于多态类型间的转换。包括继承类间的转换和兄弟类间的转换。
static_cast 用于非多态类型的转换。既不具有继承关系的类型间的转换。
*/