June 2018
Intermediate to advanced
348 pages
8h 45m
English
Classic C++ had some kind of ad-hoc syntax for the initialization of variables. Modern C++ supports uniform initialization (we have already seen examples in the type inference section). The language provides helper classes to developers to support uniform initialization for their custom types:
//----------------Initialization.cpp#include <iostream>#include <vector>#include <initializer_list>using namespace std;template <class T>struct Vector_Wrapper { std::vector<T> vctr; Vector_Wrapper(std::initializer_list<T> l) : vctr(l) {} void Append(std::initializer_list<T> l) { vctr.insert(vctr.end(), l.begin(), l.end());}};int main() { Vector_Wrapper<int> vcw = {1, 2, 3, 4, 5}; // list-initialization vcw.Append({6, ...Read now
Unlock full access