给宝宝的作业

来源:互联网 发布:软件体系结构 张友生 编辑:程序博客网 时间:2024/04/27 21:43

schema.h:

# ifndef _SCHEMA_H_
# define _SCHEMA_H_

# include "stdafx.h"
# include "stddef.h"
using namespace std;


// 这里的宏中的s为类变量或者结构体
// y是这种的变量类型,
// x为变量名称!
# define SCHEMAT(x,y,s) sizeof(y) , offsetof(s,x) , sizeof(( (s*)(0) )->x)/sizeof(y)

class schema
{
public:
//char* name;
struct schemakobe
{
 schemakobe*  schemanext;
 int  size;
 int  offset;
 int  elem;
 char* unit;
 char* comment;
};
schema( );
~schema(  );
void copydata( void* source , void* des , int size , schemakobe* sche );
schemakobe*  nextrecord( schemakobe*  current );
};
# endif

 

diceng.cpp:

// diceng.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "schema.h"
# define NUM 10
using namespace std;

class libo : public schema
{
public:
 int  age;
 char gender[7];
 char gf[7];
 libo( )
 {
  cout << "李博宝宝正在成功构建!" << endl;
 }
 ~libo(  )
 {
  cout << "李博宝宝正在离开!" << endl;
 }
 libo& operator = ( const libo& source) ;

};

    schema::schemakobe libodangan[] =
 {
  {NULL , SCHEMAT( age , int , libo ) , "year" , "李博的年龄"},
  {NULL , SCHEMAT( gender , char , libo) , "" , "李博的性别" },
  {NULL , SCHEMAT( gf , char , libo ) , "" , "李博的女朋友"  },
  {0}
 };

libo& libo::operator =(const libo& source)
{

 if( &source == NULL )
 {
  cout << "没有需要复制的东西!" << endl;
 }
 else
 {
  copydata( (void*)this , (void*)&source , sizeof( source ) ,libodangan);
 }
 return *this;
}
int _tmain(int argc, _TCHAR* argv[])
{
 
 libo libobaobao;
 libobaobao.age = 24;
 strcpy_s( libobaobao.gender , sizeof("男性") , "男性");
 strcpy_s( libobaobao.gf , sizeof("小溪儿") , "小溪儿");

 cout << libobaobao.age;
 cout << endl;
 cout << libobaobao.gender;
 cout << endl;
 cout << libobaobao.gf;
 cout << endl;
 libo liboxiaoxidebaobao;
 liboxiaoxidebaobao = libobaobao;
 cout << liboxiaoxidebaobao.age << endl;
 cout << liboxiaoxidebaobao.gender << endl;
 return 0;
}

 

schema.cpp:

# include "stdafx.h"
# include "schema.h"

using namespace std;


schema::schema(){}

schema::~schema(){}

void schema::copydata(void *des, void *source, int size , schemakobe* sche)
{
 schemakobe*  temp = sche;

 if( sche == NULL )
 {
  cout << "该类中的内容为空!" << endl;
 }
 
 else
 {
  //if( sche->schemanext != NULL )
  //{
  while( temp != NULL )
  {
   for( int i = 0 ; i < temp->elem ; i++)
   {
    if( temp->schemanext == NULL )
    {
     memcpy( (char*)des + temp->offset + i * temp->size , (char*)source + temp->offset + i * temp->size , temp->size );
    }
    else
    {
     //xiaotemp = nextrecord( temp );
     copydata( (char*)source + temp->offset , (char*)des + temp->offset , temp->size , temp->schemanext);
    }
    
   }
   temp = nextrecord( temp );
    if( temp == NULL )
    {
     break;
    }
  }
  //}
 }
}

schema::schemakobe*  schema::nextrecord(schema::schemakobe *current)
{
 if( current == NULL )
 {
  return NULL;
 }
 current++;
 if( current->size == 0 )
 {
  return NULL;
 }
 else
 {
  return current;
 }
}

 

实现数据的相互转换,同时还要谢大哥O(∩_∩)O~