小编dsc*_*tti的帖子

C++ 复制构造函数:我必须拼写出初始值设定项列表中的所有成员变量吗?

我有一些非常复杂的物体。它们包含其他对象的成员变量。我理解复制构造函数级联的美妙之处,这样默认的复制构造函数通常就可以工作。但是,最常破坏默认复制构造函数的情况(对象包含一些成员变量,它们是指向其其他成员变量的指针)仍然适用于我构建的很多内容。这是我的一个对象、它的构造函数和我编写的复制构造函数的示例:

struct PhaseSpace {
  PhaseSpace();

private:
  std::string file_name;              ///< Name of the file from which these coordinates (and
                                      ///<   perhaps velocities) derived.  Empty string indicates
                                      ///<   no file.
  int atom_count;                     ///< The number of atoms in the system
  UnitCellType unit_cell;             ///< The type of unit cell
  Hybrid<double> x_coordinates;       ///< Cartesian X coordinates of all particles
  Hybrid<double> y_coordinates;       ///< Cartesian Y coordinates of all particles
  Hybrid<double> z_coordinates;       ///< Cartesian Z coordinates of all particles
  Hybrid<double> box_space_transform; ///< Matrix to transform coordinates …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor initializer-list

9
推荐指数
1
解决办法
1594
查看次数

这种按位转换安全吗?

我遇到一种情况,需要将 16 位打包成 64 位数字,然后将它们作为 [ -32768, 32768 ) 范围内的有符号整数读回。我为此选择的方法是将数字计算为有符号 16 位 int,立即将其转换为无符号 16 位 int,然后将其向上转换为 64 位无符号 int,然后执行适当的位移以获得将关键的 16 位放入正确的位置。

这是创建位打包排列的伪代码:

Given int x, y such that x - y >= -32768 and y - x < 32768;
const int MASK_POS = 45;
const unsigned short int u_s = x - y;
unsigned long long int ull_s = u_s;
ull_s <<= MASK_POS;
Run Code Online (Sandbox Code Playgroud)

这是提取原始数字差异的伪代码:

Given unsigned long long int ull_s with 16 bits encoding a signed integer in the 46th …
Run Code Online (Sandbox Code Playgroud)

c++ signed bit-manipulation unsigned-integer

7
推荐指数
1
解决办法
140
查看次数