How can I make class to accept array of variables?

How can I make my C++ class to accept Array of variables? Like this:
Vertex<float> vertex = { 0.3, 0.2, 0.1 };
Last edited on
You're referring to an std::initializer_list.
You'll notice one of the constructors for std::vector, for example, takes in an std::initializer_list.
https://en.cppreference.com/w/cpp/container/vector/vector (8)

To know in what way you should implement it yourself depends on your data.
What does your data look like?

If your class just looks like
1
2
3
4
5
6
class Vertex
{
   T x;
   T y;
   T z; 
};

then you can just do
Vertex<float> vertex { 0.3, 0.2, 0.1 };

Do you really need a = ?

If your vertex can be an arbitrary number of dimensions, then an initializer_list is more appropriate.

PS: Prefer double by default instead of float, unless you're working specifically with interfaces that require floats (single-precision).
Thank you!
Topic archived. No new replies allowed.