【发布时间】:2017-02-01 17:41:54
【问题描述】:
我试图在网站上查找此内容,但找不到我需要的确切内容。
基本上我需要知道在默认构造函数中初始化模板变量的正确方法是什么。
例如:
template<typename T>
class myClass{
T *arr; // no problem with this.
int size;
int capacity;
T def_value; // how do I initialize this template variable in the constructor?
我尝试过类似的方法:
myClass(): arr(0), size(0), capacity(0), def_value(0){};
但它无法编译,因为我无法将0 分配给例如一个字符(我知道这一点)。
我应该如何正确初始化def_value?
【问题讨论】:
-
我不能将 0 分配给例如 char(我知道) 这不是真的。你当然可以用
0初始化char。 -
是的,我的错,它确实编译但我得到了 EXC_BAD_ACCESS(code=1, address=0x0)。
-
这很可能是因为您没有正确初始化
arr。 -
我该怎么做?
-
请发帖minimal reproducible example。这将对潜在的回答者有所帮助。
标签: c++ class templates constructor initialization-list