正在使默认构造函数和初始值设定项列表并行工作
Getting default constructor and initializer list to work side by side
我一直在使用初始值设定项列表来实例化结构的实例,但现在想添加一个默认构造函数。
struct Size {
unsigned int width;
unsigned int height;
};
void SizeFunc(Size const &size) { }
int main() {
SizeFunc({1024, 768}); // OK.
}
不幸的是,在使用初始值设定项列表进行实例化时,添加默认构造函数会导致错误。
struct Size {
Size() : width(1920), height(1080) { }
unsigned int width;
unsigned int height;
};
void SizeFunc(Size const &size) { }
int main() {
Size size; // OK.
SizeFunc({1024, 768}); // error: no matching function for call to
// 'Size::Size(<brace-enclosed initializer list>)'
}
我需要添加什么构造函数才能工作?我曾尝试使用std::initializer_list的构造函数,但到目前为止没有成功。
只需添加一个采用两个无符号int的非显式构造函数,该构造函数使用参数初始化成员。
Size(unsigned int width, unsigned int height) : width(width), height(height) {}
相关文章:
- 范围 v3 可以包含初始值设定项列表的工作吗?
- 从列表向量中删除无法按预期工作
- emplace_back初始化列表错误,当初始化列表在独立变量上工作时
- 我怎样才能使这个列表工作
- 具有可变参数的std ::函数列表如何工作
- 在为工作线程访问 lambda 中捕获的向量列表中的元素引用时,是否需要互斥锁?
- 如何使用字符串更改此插入功能以在链接列表中工作
- QDIR入口列表可以使用文件夹快捷方式透明地工作
- 常量变量初始化仅与成员初始化列表一起工作
- Photoshop插件sdk示例列表不工作
- 如何使此链接列表正常工作
- 初始化列表中的尝试/捕获如何工作
- 列表函数未按预期工作
- 初始值设定项列表程序运行良好,但直接赋值时无法正常工作
- 我在 C++ 中创建了一个列表容器类,但它没有按预期工作
- 通过修改二叉搜索算法来改进它,使其在搜索大量单词(单词列表)中的单词时工作得更快
- 列表:<T>:迭代器无法正常工作
- 访问列表项时的 UI 和工作线程同步
- 邻接列表表示形式 C++,使用类和指针,指向无法正常工作的列表
- C++对象列表无法正常工作