
请教c++高手,帮忙改改,急用,在线等,谢谢了!
#include<iostream>usingnamespacestd;structBase1{intx;};structBase2{floaty;};structDer...
#include<iostream>
using namespace std;
struct Base1{int x;};
struct Base2{float y;};
struct Derived:Base1,Base2{};
int main()
{
Derived*pd=new Derived;
pd->x=1;pd->y=2.0f;
void * pv=pd;
Base2 * pd=static_cast<Base2* >(pv);
cout<<pd->y<<" "<<pd->y<<endl;
delete pd;
return 0;
} 展开
using namespace std;
struct Base1{int x;};
struct Base2{float y;};
struct Derived:Base1,Base2{};
int main()
{
Derived*pd=new Derived;
pd->x=1;pd->y=2.0f;
void * pv=pd;
Base2 * pd=static_cast<Base2* >(pv);
cout<<pd->y<<" "<<pd->y<<endl;
delete pd;
return 0;
} 展开
展开全部
这个程序有很多问题,但是太简单导致我不知道你的明确意图。
1.void * pv=pd;是可以赋值的,但是用pv->x就错了,因为pv不能识别结构体找不到X的位置。
2.void * pv=pd;Base2 * pd=static_cast<Base2* >(pv);如果你想把Derived指针转成Base2指针,强转就可以了,Base2* pf = (Base2*)pd;
3.cout<<pd->y<<" "<<pd->y<<endl;上面出现两个pd,编译器不知道使用哪一个,就算要用,肯定是同一个。
我估计一下你是不是想要这样子:
Derived* pd=new Derived;
pd->x=1;
pd->y=2.0f;
// void * pv=pd;多余
Base2 * pf = (Base2*)pd;// Base2 * pd=static_cast<Base2* >(pv);都是强转
cout<<pf->y/*<<" "<<pd->y*/<<endl;
delete pd;
return 0;
1.void * pv=pd;是可以赋值的,但是用pv->x就错了,因为pv不能识别结构体找不到X的位置。
2.void * pv=pd;Base2 * pd=static_cast<Base2* >(pv);如果你想把Derived指针转成Base2指针,强转就可以了,Base2* pf = (Base2*)pd;
3.cout<<pd->y<<" "<<pd->y<<endl;上面出现两个pd,编译器不知道使用哪一个,就算要用,肯定是同一个。
我估计一下你是不是想要这样子:
Derived* pd=new Derived;
pd->x=1;
pd->y=2.0f;
// void * pv=pd;多余
Base2 * pf = (Base2*)pd;// Base2 * pd=static_cast<Base2* >(pv);都是强转
cout<<pf->y/*<<" "<<pd->y*/<<endl;
delete pd;
return 0;
展开全部
void * pv=pd;
Base2 * pd=static_cast<Base2* >(pv);
pd重定义了
Base2 * pd=static_cast<Base2* >(pv);
pd重定义了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-04-24
展开全部
你想输出什么结果呢?
追问
#include
using namespace std;
struct Base1{int x;};
struct Base2{float y;};
struct Derived:Base1,Base2{};
int main()
{
Derived*pd=new Derived;
pd->x=1;pd->y=2.0f;
void * pv=pd;
Base2 * pb=static_cast(pv);
coutyy<<endl;
delete pd;
return 0;
}看这个程序,刚刚那个打错了两个地方
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询