"尾随数据"

//DataSource.h

#ifndef _DATASOURCE_H
#define _DATASOURCE_H

struct tagData
{
	int nID;
	double dValue;
	char szRef[128];
};

void ProvideDataSource(tagData* data)
{
	static int id = 0;

	data->nID = ++id;
	data->dValue = id;
	itoa(id, data->szRef, 10);
}

#endif

//TrailingData.cpp

#include <iostream>
#include "DataSource.h"

using namespace std;

struct tagMyData
{
	tagData Data; //原始struct一定要放在第一位
	char Type;
	bool IsValid;
};

int main(int argc, char* argv[])
{
	tagData* data = new tagData(); //原始struct
	ProvideDataSource(data);
	cout<<"nID: "<<data->nID<<", dValue: "<<data->dValue<<", szRef: "<<data->szRef<<endl;

	tagMyData* myData = new tagMyData(); //添加了"尾随数据"的新struct
	myData->Type = 'Y';
	myData->IsValid = true;
	ProvideDataSource((tagData*)myData);
	cout<<"nID: "<<myData->Data.nID<<", dValue: "<<myData->Data.dValue<<", szRef: "<<myData->Data.szRef<<endl;

	return 0;
}

/*
输出:

	nID: 1, dValue: 1, szRef: 1
	nID: 2, dValue: 2, szRef: 2
*/


你可能感兴趣的:(尾随数据)