简体   繁体   English

C++ POD初始化

[英]c++ POD initialization

I've read about POD objects in C++.我已经阅读了 C++ 中的 POD 对象。 I wanna have a POD struct to be written into a file.我想将 POD 结构写入文件。 So it should have only public data with no ctors/dtors etc. But as far as i know it can have static function in it.所以它应该只有公共数据,没有 ctors/dtors 等。但据我所知,它可以有静态功能。 So can I use "named constructor idiom" here?那么我可以在这里使用“命名构造函数成语”吗? I need dynamic initialization, but I don't want to duplicate arguments checking at every struct initialization Here is a simple example (it's just simple example, not a working code):我需要动态初始化,但我不想在每个结构体初始化时重复检查参数这里是一个简单的例子(它只是简单的例子,不是工作代码):

struct A
{
  int day;
  int mouth;
  int year;

   static A MakeA(const int day, const int month, const int year)
   {  
      // some simple arguments chech
      if ( !(day >= 1 && day <= 31) || !(month >=1 && month <=12) || !(year <= 2010) )
         throw std::exception();

      A result;
      result.day = day;
      result.month = month;
      result.year = year;
      return result;
   }
};

So I have some kind of a constructor and a POD structure that I can simply fwrite to a file?所以我有某种构造函数和 POD 结构,我可以简单地将其写入文件? It it correct?它正确吗?

That should be fine.那应该没问题。

You can even have a non-static member functions (as long as they are not virtual)你甚至可以有一个非静态成员函数(只要它们不是虚拟的)

You cannot have something that is called automatically (like ctor/dtor).你不能有自动调用的东西(比如ctor/dtor)。 Thingsthat you explicitly call are fine.你明确调用的东西很好。

If you write the stream operators it makes life a lot simpler.如果你编写流操作符,它会让生活变得更简单。
Its not as if writing in binary is significantly faster (as you need to write the code to convert for different endian formats) and space nowadays is practically irrelevant.并不是说用二进制编写会明显更快(因为您需要编写代码以转换为不同的字节序格式),而且现在空间几乎无关紧要。

struct A
{
  int day;
  int mouth;
  int year;

   A(const int day, const int month, const int year)
   {  
      // some simple arguments chech
      if ( !(day >= 1 && day <= 31) || !(month >=1 && month <=12) || !(year <= 2010) )
         throw std::exception();

      this->day    = day;
      this->month  = month;
      this->year   = year;
   }
};
std::ostream& operator<<(std::ostream& str, A const& data)
{
    return str << data.day << " " << data.month << " " << data.year << " ";
}
std::istream& operator>>(std::istream& str,A& data)
{
    return str >> data.day >> data.month >> data.year;
}

With this defined the whole plethera of standard algorithms becomes available and easy to use.有了这个定义,大量的标准算法变得可用且易于使用。

int main()
{
    std::vector<A>    allDates;
    // Fill allDates with some dates.

    // copy dates from a file:
    std::ifstream  history("plop");
    std::copy(std::istream_iterator<A>(history),
              std::istream_iterator<A>(),
              std::back_inserter(allDates)
             );

    // Now  save a set of dates to a file:
    std::ofstream  history("plop2");
    std::copy(allDates.begin(),
              allDates.end(),
              std::ostream_iterator<A>(history)
             );
}

You are correct.你是对的。 That's just an ordinary old piece of data.那只是一个普通的旧数据。 No funny virtual table pointers or anything like that in it.没有有趣的虚拟表指针或类似的东西。

Now, I'm still not sure it's all that good an idea to simply use fwrite to write the data to a file.现在,我仍然不确定简单地使用fwrite将数据写入文件是否是个好主意。 You can do that and fread the data back in provided that the program that does the fread is written with the same version of the compiler used to do the fwrite in the first place.你可以做到这一点, fread的条件是该做的程序把数据传回fread与相同版本用来做编译器的书面fwrite摆在首位。 But if you switch compilers, platforms, or sometimes even versions, that may change.但是,如果您切换编译器、平台,有时甚至版本,情况可能会发生变化。

I would suggest something like Protocol Buffers to do the work of making your data structure persistent.我建议使用Protocol Buffers 之类的东西来完成使数据结构持久化的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM