在c++类中初始化静态结构
Initilialize static struct in C++ Class
我想初始化静态结构体std_msgs::ColorRGBA。不幸的是,它没有构造函数来初始化它的4个float成员,因为它不是我自己的类,我不能修改它的构造函数。
class A {
static std_msgs::ColorRGBA white; // struct with 4 float members;
};
如何初始化静态成员white?我不能使用c++11
我试着
std_msgs::ColorRGBA A::white = { 1.0f, 1.0f, 1.0f, 1.0f};
结构体看起来像:
template <class T>
struct ColorRGBA_ {
typedef ColorRGBA_<T> Type;
ColorRGBA_(): r(0.0), g(0.0), b(0.0), a(0.0) { }
ColorRGBA_(const ContainerAllocator& _alloc) : r(0.0), g(0.0) , b(0.0), a(0.0) { }
typedef float _r_type;
_r_type r;
typedef float _g_type;
_g_type g;
typedef float _b_type;
_b_type b;
typedef float _a_type;
_a_type a;
typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> > Ptr;
typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> const> ConstPtr;
boost::shared_ptr<std::map<std::string, std::string> > __connection_header;
};
你可以做一个init函数(例如,ColorRGBA_init
),就像下面的例子:
#include <iostream>
namespace std_msgs {
struct ColorRGBA {
double r;
double g;
double b;
double a;
};
}
std_msgs::ColorRGBA ColorRGBA_init(double const r = 1.0, double const g = 1.0, const double b = 1.0, double const a = 1.0) {
std_msgs::ColorRGBA out;
out.r = r;
out.g = g;
out.b = b;
out.a = a;
return out;
}
class A {
public:
static std_msgs::ColorRGBA white; // struct with 4 float members;
};
std_msgs::ColorRGBA A::white = ColorRGBA_init();
int main() {
std::cout << A::white.r << std::endl;
}
为该类型编写任意init函数,例如:
std_msgs::ColorRGBA GenerateColorRGBA() {
std_msgs::ColorRGBA color;
color.r = 1.0f;
color.g = 1.0f;
color.b = 1.0f;
color.a = 1.0f;
return color;
}
std_msgs::ColorRGBA A::white = GenerateColorRGBA();
不知道为什么该结构被模板化或需要这样声明,因为它不是聚合,并且您不能初始化列表初始化它,您应该使用初始化函数或方法,如以下
#include <iostream>
using namespace std;
namespace std_msgs {
template <class T>
struct ColorRGBA_ {
typedef ColorRGBA_<T> Type;
ColorRGBA_(): r(0.0), g(0.0), b(0.0), a(0.0) { }
//ColorRGBA_(const ContainerAllocator& _alloc) : r(0.0), g(0.0) , b(0.0), a(0.0) { }
typedef float _r_type;
_r_type r;
typedef float _g_type;
_g_type g;
typedef float _b_type;
_b_type b;
typedef float _a_type;
_a_type a;
/*typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> > Ptr;
typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> const> ConstPtr;
boost::shared_ptr<std::map<std::string, std::string> > __connection_header;*/
};
typedef ColorRGBA_<float> ColorRGBA;
}
class A {
public:
static std_msgs::ColorRGBA white; // struct with 4 float members;
};
// std_msgs::ColorRGBA A::white = {1.0f,1.0f,1.0f,1.0f}; // Just works for aggregates
std_msgs::ColorRGBA initializeMe() {
std_msgs::ColorRGBA obj;
obj.r = 22.0f;
// ..
return obj;
}
std_msgs::ColorRGBA A::white = initializeMe();
int main()
{
cout << A::white.r;
}
http://ideone.com/7LOlOe 相关文章:
- 静态结构和一个定义规则
- 类的静态结构指针声明在C++
- 无法使用 strcpy 将字符串复制到静态结构成员
- 'class MyClass' 在尝试定义静态结构实例时不会命名类型
- constexpr静态结构类成员的声明冲突
- C 98如何在标题中初始化静态结构阵列
- C 类静态结构成员定义
- C++中静态结构的初始化
- 有没有办法将数组行为作为静态结构数据成员?
- C++静态结构类型成员初始化
- 类内定义的静态结构的静态成员的编译时默认值
- 从具有静态结构的基类派生
- 从静态函数访问非静态结构
- 匿名命名空间中的静态结构
- C++静态结构init-item没有命名类型
- 静态结构-定义、对象、成员
- 静态结构和正常结构的区别是什么
- 如何重置函数内部的静态结构
- c++中的静态结构和静态全局变量
- c++静态结构模板方法返回枚举类型