给宝宝的作业

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~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值