Using std::initializer_list in constructors to initialize member variables

In this article we will discuss how to use std::initializer_list<T> to initialize member variables.

Suppose we have a class Point that has 3 member variables representing x, y & Z axis coordinates. It has a parameterized constructor too i.e.

class Point {
	int mX;
	int mY;
	int mZ;
public:
	Point(int a, int b, int c) :
			mX(a), mY(b), mZ(c) {
	}
	void display() {
		std::cout << "(" << mX << "," << mY << "," << mZ << ")\n";
	}
};

We can create a object of Point by passing std::initializer_list<int> i.e. {1,2,3} as argument i.e.

 

// Calling Point's constructor with initializer_list
// It will call the parameterized constructor with 3 arguments, because number of
// elements in initializer_list is also three.
Point pointobj( { 1, 2, 3 });

Compiler will automatically create a std::initializer_list<int> object refereing to {1,2,3} & the search for a constructor that accepts a std::initializer_list<int> as an argument, if not then any constructor that accepts 3 ints.

Checkout complete example as follows,

#include <iostream>
#include <vector>

class Point {
	int mX;
	int mY;
	int mZ;
public:
	Point(int a, int b, int c) :
			mX(a), mY(b), mZ(c) {
	}
	void display() {
		std::cout << "(" << mX << "," << mY << "," << mZ << ")\n";
	}
};

int main() {
	// Calling Point's constructor with initializer_list
	// It will call the parameterized constructor with 3 arguments, because number of
	// elements in initializer_list is also three.
	Point pointobj( { 1, 2, 3 });

	pointobj.display();

	return 0;
}

Output:
(1,2,3)

But what if we have an another constructor that accepts a std::initializer_list<int> as an argument i.e.
// Constructor with initializer_list as an argument
Point::Point(std::initializer_list<int> list)
{
	if(list.size() == 3)
	{
		std::initializer_list<int>::iterator it = list.begin();
		mX = *it++;
		mY = *it++;
		mZ = *it;
	}
	std::cout << "Point:: Initializer_list<int>Constructor\n";
}

In this case when we will create a Point object with {1,2,3} it will always give preference to constructor with std::initializer_list<int> i.e.

Advertisements

 

// Calling Point's constructor with initializer_list
// It will call the parameterized constructor with 3 arguments, because number of
// elements in initializer_list is also three.
Point pointobj1( { 1, 2, 3 });

So, to call the other constructor we need to call the constructor using original way i.e.
// Will call the constructor with 3 ints
Point pointobj2(4,5,6);

Complete example is as follows,
#include <iostream>
#include <vector>
#include <initializer_list>

class Point {
	int mX;
	int mY;
	int mZ;
public:
	// Constructor with 3 ints as argument
	Point(int a, int b, int c) :
			mX(a), mY(b), mZ(c) {
		std::cout << "Point:: Parameterized Constructor\n";
	}
	// Constructor with initializer_list as an argument
	Point(std::initializer_list<int> list)
	{
		if(list.size() == 3)
		{
			std::initializer_list<int>::iterator it = list.begin();
			mX = *it++;
			mY = *it++;
			mZ = *it;
		}
		std::cout << "Point:: Initializer_list<int>Constructor\n";
	}

	void display() {
		std::cout << "(" << mX << "," << mY << "," << mZ << ")\n";
	}
};

int main() {
	// Calling Point's constructor with initializer_list
	// It will call the parameterized constructor with 3 arguments, because number of
	// elements in initializer_list is also three.
	Point pointobj1( { 1, 2, 3 });

	// Will call the constructor with 3 ints
	Point pointobj2(4,5,6);

	pointobj1.display();
	pointobj2.display();

	return 0;
}


Output
Point:: Parameterized Constructor
(1,2,3)
(4,5,6)

Passing variable arguments with std::initialer_list<T>

We can also pass variable arguments to a constructor or any other function that accepts std::initializer_list<T> as an argument i.e.

#include <iostream>
#include <vector>
#include <initializer_list>

class Point {
	std::vector<int> arr;
public:
	//Constructor accepts a initializer_list as argument and
	// Initialize the member vector with it.
	Point(const std::initializer_list<int> & list) : arr(list)
	{}

	void display() {
		for(int i : arr)
			std::cout<<i<<" , ";
		std::cout<<std::endl;
	}
};

int main() {
	Point pointobj( { 1, 2, 3, 4, 5 });

	pointobj.display();

	return 0;
}


Output:
1 , 2 , 3 , 4 , 5 ,

 

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top