求一个例子代码,说明派生类到基类的向上转换.

fly2008_fly1314 2008-04-28 09:14:39
编写一个程序,说明派生类到基类的向上转换.用static_cast执行此向上转换.
这是书上的原题,不懂啊,什么是向上转换,能写个例子说明下吗,谢谢了.
...全文
67 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2012-03-31
  • 打赏
  • 举报
回复
static_cast和c中的最普通的转型差不多。

参考:
区别:static _cast、dynamic _cast、reinterpret_cast和const_cast
red64321 2012-03-31
  • 打赏
  • 举报
回复
不会,学习学习
meiZiNick 2008-05-01
  • 打赏
  • 举报
回复
不会,帮顶
effective_person 2008-04-28
  • 打赏
  • 举报
回复

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;
}

fly2008_fly1314 2008-04-28
  • 打赏
  • 举报
回复
谢谢,我好好消化下先..
effective_person 2008-04-28
  • 打赏
  • 举报
回复

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
}




fly2008_fly1314 2008-04-28
  • 打赏
  • 举报
回复
/*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;
}

照书上写的,最上面的注释找的网上的,
1.static_cast和dynamic_cast运算符什么区别,
2.向上转换是不是就是一个规则,表示派生类指针是可以转换为基类指针的?
3.派生类指针被转成基类指针了是不是,用这个指针在去调用派生类自己的个性函数就不行了啊?
effective_person 2008-04-28
  • 打赏
  • 举报
回复

#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 用于非多态类型的转换。既不具有继承关系的类型间的转换。

*/

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧