Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用元数据/继承跨多个类分解代码_C++_Templates_Inheritance_Metadata_Metaprogramming - Fatal编程技术网

C++ 使用元数据/继承跨多个类分解代码

C++ 使用元数据/继承跨多个类分解代码,c++,templates,inheritance,metadata,metaprogramming,C++,Templates,Inheritance,Metadata,Metaprogramming,我有两个类,它们代表两个非常简单的数据库,每个类都有一个“Save”函数,可以将类中的内容写入一个文件。由于“Save”函数中的代码非常相似,我想知道是否可以将其分解出来 我的一位同事说,这可能与继承和/或元数据有关,所以我试着自己用谷歌进行研究。然而,我找不到任何有用的东西,仍然不确定我想做的事情是否可能 如果可能的话,我想我需要让另一个类或函数知道每个类的类型,并以某种方式迭代它们(元数据?)。它将检查每个数据的类型,并根据类型,确保正确输出到文本文件 (我知道姓名、年龄等数据应该是私有的,

我有两个类,它们代表两个非常简单的数据库,每个类都有一个“Save”函数,可以将类中的内容写入一个文件。由于“Save”函数中的代码非常相似,我想知道是否可以将其分解出来

我的一位同事说,这可能与继承和/或元数据有关,所以我试着自己用谷歌进行研究。然而,我找不到任何有用的东西,仍然不确定我想做的事情是否可能

如果可能的话,我想我需要让另一个类或函数知道每个类的类型,并以某种方式迭代它们(元数据?)。它将检查每个数据的类型,并根据类型,确保正确输出到文本文件

(我知道姓名、年龄等数据应该是私有的,但为了保持简单,我只是让一切都公开了)


一个可能的解决方案是使用元编程(不确定元数据是什么意思),即使用模板重用公共部件

template<typename T1, typename T2>
void TSave(const std::string fname, const T1& p1, const T2& p2) {
    std::string filename = fname;
    std::stringstream data;

    data << p1 << "\n";
    data << p2 << "\n";

    std::ofstream outfile(filename);
    outfile.write(data.str().c_str(), data.str().size());
    outfile.close();
}

class A {
  ...

  void Save(void) {
    TSave("A.txt", name, age);
  }

  std::string name;
  int age;
};

class B {
  ...

  void Save(void) {
    TSave("B.txt", ID, points);
  }

  int ID;
  int points;
};
模板
void TSave(常数std::字符串fname、常数T1和p1、常数T2和p2){
std::string filename=fname;
std::stringstream数据;

数据您要查找的是:将对象保存到文件中(并在某一天还原对象)

当然,你可以编写你自己的序列化框架,马珂的答案在这个方向上是一个有趣的开始。但是,你也可以考虑现有的库,例如:

#包括
#包括
甲级{
私人:
好友类boost::serialization::access;
模板
无效序列化(存档和ar,常量未签名整数版本)
{
ar&name;
ar&age;
}
...
};
B类{
私人:
好友类boost::serialization::access;
模板
无效序列化(存档和ar,常量未签名整数版本)
{
ar&ID;
ar&points;
}
...
};
main(){
A A;
B B;
...
{
std::ofs流(“myfile”);
boost::archive::text_oarchive arch(ofs);

arch非常好。你能详细说明一下,并提出一个版本,它可以有更多的参数,而不是固定数量的2吗?@Christophe我不知道你可能需要多少额外的参数。添加更多的参数或使用a应该非常简单。我不知道OP可能有多少参数。但这是不可能的它总是正好是2…我希望你能展示如何使用variadics。但没关系,一个例子两个答案都非常有用,可惜我只能选择一个。感谢你和Marco
template<typename T1, typename T2>
void TSave(const std::string fname, const T1& p1, const T2& p2) {
    std::string filename = fname;
    std::stringstream data;

    data << p1 << "\n";
    data << p2 << "\n";

    std::ofstream outfile(filename);
    outfile.write(data.str().c_str(), data.str().size());
    outfile.close();
}

class A {
  ...

  void Save(void) {
    TSave("A.txt", name, age);
  }

  std::string name;
  int age;
};

class B {
  ...

  void Save(void) {
    TSave("B.txt", ID, points);
  }

  int ID;
  int points;
};
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class A {
private: 
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & name;
        ar & age;
    }
...
};
class B {
private: 
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & ID;
        ar & points;
    }
...
};
main() {
   A a; 
   B b; 
   ...
   {
      std::ofstream ofs("myfile");
      boost::archive::text_oarchive arch(ofs);
      arch << a << b;
   }
}