我正在编写一些具有大量相当简单的对象的代码,我希望它们可以在编译时创建.我认为编译器能够做到这一点,但我无法弄清楚如何.
在C中,我可以执行以下操作:
#include <stdio.h>
typedef struct data_s {
int a;
int b;
char *c;
} info;
info list[] = {
1, 2, "a",
3, 4, "b",
};
main()
{
int i;
for (i = 0; i < sizeof(list)/sizeof(*list); i++) {
printf("%d %s\n", i, list[i].c);
}
}
Run Code Online (Sandbox Code Playgroud)
使用#C++*每个对象都有一个被调用的构造函数,而不仅仅是在内存中进行布局.
#include <iostream>
using std::cout;
using std::endl;
class Info {
const int a;
const int b;
const char *c;
public:
Info(const int, const int, const char *);
const int get_a() { return …Run Code Online (Sandbox Code Playgroud)