0

I wanted to know how can the initializer list constructors be implemented if I want to make my own vector class?

eg:

Vector_Class<int> v{1, 2, 3, 4, 5};

How can this type of constructor be implemented?

1
  • Did the answer help? Please ask if you want me to clarify something. Commented Dec 23, 2021 at 0:27

1 Answer 1

5

You declare a constructor that takes a std::initializer_list<some_type>.

Example:

#include <iostream>
#include <initializer_list>

template<class T>
class foo {
public:
    foo(std::initializer_list<T> Ts) {
        for(auto v : Ts) {
            std::cout << v << '\n';
        }
    }
};

int main() {
    foo<int> x{1,2,3,4,5};
}

Output:

1
2
3
4
5
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.