我可以初始化 const 实例,以便我可以将其用作 const 来初始化数组吗?
can i initialize const instance so that i can use it as const to initialize array?
(小参考(:
template<typename T>
struct IVector2 {
T x, y;
IVector2(T x, T y) :
x(x), y(y) { }
};
有没有办法做到这一点:
static const IVector2<int> floors; // lowest / highest floor
std::vector<std::array<IPerson, floors.x - floors.y>> requestQueue;
我需要初始化数组大小以使其在 floor.x 和 floor.y 之间有所不同。 我一直在寻找有关如何初始化楼层的答案,我得到了这个
const IVector2<int> IElevatorHandler::floors(-1, 4);
但这并不能做到这一点。
您可以尝试为此使用文字类。我对您的代码进行了一些更改
template<typename T>
struct IVector2 {
T x, y;
constexpr IVector2(T x, T y) : //constexpr constructor
x(x), y(y) { }
};
static constexpr IVector2<int> floors(10,1); // Initialized constexpr object. Now available at compile time
相关文章:
- 在内存不足之前,我可以声明多少个 const 变量?
- 如果 const 不分配内存,为什么我可以获取 const 的地址?
- 如果我在 const 函数上使用指针,我可以返回什么?
- 我可以初始化 const 实例,以便我可以将其用作 const 来初始化数组吗?
- 为什么我可以调用一个从const方法更改成员的方法
- 我可以将"token pasting operator"与"const"模板参数一起使用吗?
- 为什么我可以更改指针的常数(const char *)槽的值
- 为什么我可以在C 11中使用const函数修改类
- 我可以在类头文件中定义一个类的const静态实例吗
- 我可以在C++中从const char*数组中进行std::字符串的零拷贝分配吗
- 如果类 A 修改其构造参数,我可以使用常量参数初始化 const A 吗?
- 为什么我可以将字符串文字初始化为const char*和QString,而不是QString*
- 我可以安全地将指向 const 成员的指针转换为相同类型但非常量
- 为什么我可以在多个包含const int的cpp文件中包含一个头文件,而不会出现编译器错误
- 我可以从引用传递的参数返回值中声明const int吗
- 为什么我可以将返回 'const T&' 的函数的返回值分配给 'T' 变量,而不是 'T&' 变量?
- 我可以在指针类型声明中省略const限定符吗?
- const 函数,但我可以"modify"类
- const char*a[4];我可以更改a[]值吗
- 我可以有一个返回*this并处理非常量对象的const成员函数吗