neoreturned
Programmer
hi everyone i hav a porgram and it is uses streams and files also inheritances....
//////////////////////
.
.
.
class base{
protected:
int a;
int b;
public:
void getdata()
{}
void FileWrite(const char* filename,int no)
{
ofstream dos(filename,ios::binary|ios::app);
dos.seekp(no*sizeof(*this));
dos.write((char*)this,sizeof(*this));
}
};
.
.
.
class derivedublic base
{
private:
int c;
public:
void FileWrite(const char* filename,int no)
{
base::FileWrite(filename,no);
}
};
.
.
.
int main()
{
derived* derobj=new derobj;
derobj->getdata();
derobj->FileWrite("FILE.DAT",0);
}
In this program derived's class FileWrite function sees this pointer as derived object and sizeof(*this)=12...(sizeof(int)=4). But base::FileWrite() function sees this derived object but sizeof(*this)=8...
and it only see base datas not derived(in this int c is derived's data...)
What is wrong and how can i fix this problem????
//////////////////////
.
.
.
class base{
protected:
int a;
int b;
public:
void getdata()
{}
void FileWrite(const char* filename,int no)
{
ofstream dos(filename,ios::binary|ios::app);
dos.seekp(no*sizeof(*this));
dos.write((char*)this,sizeof(*this));
}
};
.
.
.
class derivedublic base
{
private:
int c;
public:
void FileWrite(const char* filename,int no)
{
base::FileWrite(filename,no);
}
};
.
.
.
int main()
{
derived* derobj=new derobj;
derobj->getdata();
derobj->FileWrite("FILE.DAT",0);
}
In this program derived's class FileWrite function sees this pointer as derived object and sizeof(*this)=12...(sizeof(int)=4). But base::FileWrite() function sees this derived object but sizeof(*this)=8...
and it only see base datas not derived(in this int c is derived's data...)
What is wrong and how can i fix this problem????