lundi 13 février 2023

How to use braced integer list initialization for constructor in c++?

I am trying but unable to create a constructor for my class which 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)

Aucun commentaire:

Enregistrer un commentaire