英文:
How to use braced integer list initialization for constructor in c++?
问题
我正在尝试但无法为我的类创建一个构造函数,该构造函数接受整数参数(以后可能更改为某种自定义数据类型),通过用大括号初始化列表,就像经典的旧int a[]{0,1,2}
一样。以下是我的代码:
#include <iostream>
using namespace std;
class class_a {
private:
int ptr;
public:
class_a() {};
template <typename ... Args>
class_a(Args&& ... args) : ptr((args)...) {}
};
int main()
{
class_a c1{0}; //工作正常
//class_a c2[]{0,1,2}; //不起作用
}
我希望输出的变量ptr
可以初始化为整数数组{0,1,2}
(只是暂时的 - 以后可能更改为任何复杂的数据类型)。
英文:
I am trying but unable to create a constructor for my class that takes in integer arguments (which may change to some custom data type later) through braced list initialization like the classic old int a[]{0,1,2}
. Here is my code
#include<iostream>
using namespace std;
class class_a {
private:
int ptr;
public:
class_a() {};
template<typename ... Args>
class_a(Args&& ... args) : ptr((args)...) {}
};
int main()
{
class_a c1{0}; //works
//class_a c2[]{0,1,2}; //doesnt works
}
I want an output where the variable ptr
can be initialized as the array of integers {0,1,2}
(again, just for now - it may change to any complex data type later)
答案1
得分: 2
由于这个类不是一个类模板(或者说没有指定“ptr”变量应该包含多少元素),我假设你真正需要的是一个std::vector<int>
,它可以按照你想要的方式进行初始化:
#include <utility>
#include <vector>
class class_a {
private:
std::vector<int> ptr;
public:
class_a() = default;
template <typename... Args>
class_a(Args&&... args) : ptr{std::forward<Args>(args)...} {}
// 使用完美转发以便以后更改为用户定义的类型
};
int main() {
class_a c1{0}; // 可行
class_a c2{0, 1, 2}; // 现在可行
}
如果你真的希望ptr
是一个数组,那么你需要将class_a
变成一个类模板:
template<size_t N>
class class_a {
private:
int ptr[N]; // 现在是一个数组
public:
class_a() = default;
template <typename... Args>
class_a(Args&&... args) : ptr{std::forward<Args>(args)...} {}
};
// 类型推导指南
template<class... Args> class_a(Args&&...) -> class_a<sizeof...(Args)>;
但请注意,具有不同N的class_a<N>
现在是不同的类型,与使用std::vector<int>
作为成员变量不同。
英文:
Since the class is not a class template (or something saying how many elements there should be in the "ptr" variable) I'm assuming that what you need is really a std::vector<int>
, which could be initialized the way you want:
#include <utility>
#include <vector>
class class_a {
private:
std::vector<int> ptr;
public:
class_a() = default;
template <typename... Args>
class_a(Args&&... args) : ptr{std::forward<Args>(args)...} {}
// with perfect forwarding in case you change to a user-defined type later
};
int main() {
class_a c1{0}; // works
class_a c2{0, 1, 2}; // now works
}
If you really want ptr
to be an array, you need to make class_a
a class template:
template<std::size_t N>
class class_a {
private:
int ptr[N]; // now an array
public:
class_a() = default;
template <typename... Args>
class_a(Args&&... args) : ptr{std::forward<Args>(args)...} {}
};
// deduction guide
template<class... Args> class_a(Args&&...) -> class_a<sizeof...(Args)>;
But note that class_a<N>
s with different N
s are now different types unlike if you use a std::vector<int>
as a member variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论