有没有办法访问复数类C++私有成员变量?
Is there any way to access private member variable of C++ complex number class?
我正在使用标准C++库标题中内置的复数类std::complex
。我在HLS工具中应用了代码。该工具无法访问该复杂类的私有成员变量。是否可以将其公开或我该怎么办?
Error: /usrf01/prog/mentor/2015-16/RHELx86/QUESTA-SV-AFV_10.4c-5/questasim/gcc-4.7.4-linux_x86_64/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.7.4/../../../../include/c++/4.7.4/complex(222):
error: 'fpml::fixed_point<int, 16u, 15u> std::complex<fpml::fixed_point<int, 16u, 15u> >::_M_real' is private
std::complex
模板有点神奇:您具有将复数重新解释为两个标量的数组的显式权限。更一般地说,以下内容是有效的:
std::complex<float> a[10];
float* r = reinterpret_cast<float*>(a);
for (int i = 0; i != 20; ++i) std::cout << r[i] << 'n';
也就是说,您可以将复数数组视为实数两倍的数组。您可以使用此方法单独访问复数的元素。
但是,请注意以下约束([complex.numbers]p2(:
实例化除
float
、double
或long double
以外的任何类型的模板complex
的效果是 未指定。
为了完整起见,访问成员的另一种方法是使用相应的getter,例如像这样
#include <complex>
int main()
{
std::complex<float> c;
c.real(1);
c.imag(2);
return c.real();
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 将包含C样式数组的对象初始化为成员变量(C++)
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 在循环中按顺序遍历成员变量
- c++类声明时,相同的例程,不同的成员变量类型
- 如何从另一个文件继承私有成员变量和公共函数
- 在C++类中,是否必须初始化作为数组的成员变量
- 如何从子成员函数修改父公共成员变量
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 从私有成员变量的成员方法返回unique_ptr
- 在派生类中使用基类的私有成员变量的最佳方法
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++:是否可以使用非静态成员变量模板?
- 打印所有继承的类成员变量和方法
- 如何在复杂继承中访问静态成员变量
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 成员变量与函数概念检查