PDA

View Full Version : سوال: مشکل با static_cast



هم دانشگاهی
دوشنبه 26 اردیبهشت 1390, 06:33 صبح
سلام بر همه !

دوستان اولا می خواستم یک توضیح در باره static_cast بدین و بعد بگین اشکال این خط کد چیه ؟


#include <iostream>
#include <conio.h>
using namespace std;
/************************************************** ****************************/
class base
{
private:
int x;
public:
base(int a);
friend ostream& operator << (ostream &out, base &ob);
};
//////////
base::base(int a)
{
x=a;
}
ostream& operator << (ostream &out, base &ob)
{
out<<" x= "<<ob.x;
return out;
}
/************************************************** ****************************/
class derived1:public base
{
private:
int y;
public:
derived1(int a,int b);
friend ostream& operator << (ostream &out, derived1 &ob);
};
//////////
derived1::derived1(int a,int b):base(a)
{
y=b;
}
ostream& operator << (ostream &out, derived1 &ob)
{
out<< static_cast <base> (ob)<<" y= "<<ob.y;
return out;
}

int main()
{
base *B=0,obB(2);
cout<<"obB: "<<obB<<endl; getch();
derived1 *A=0,ob1(2,3);
B=&ob1;
A=static_cast <derived1*> (B);
cout<<"ob1: "<<*A<<endl;
getch();
return 0;
}


در واقع از خط 39 error میگیره !
ممنون

هم دانشگاهی
دوشنبه 26 اردیبهشت 1390, 20:56 عصر
کسی نبود به ما کمک کنه ؟!؟!؟!؟!؟! :گریه:

در واقع من سوالم اینه که چرا static_cast تو DEV یا TC کار نمیکنه ؟! :متفکر: