jeudi 14 octobre 2021

C++ Initialization types - practical use

According to cppreference there are following initialization types:

  • Value initialization, e.g. std::string s{};

  • Direct initialization, e.g. std::string s("hello");

  • Copy initialization, e.g. std::string s = "hello";

  • List initialization, e.g. std::string s{'a', 'b', 'c'};

  • Aggregate initialization, e.g. char a[3] = {'a', 'b'};

  • Reference initialization, e.g. char& c = a[0];

From time to time reading about something I need to understand about C++ I run into one of these types, go to this page and honestly never gain understanding of all those different types and more importantly should I really care about it? And so far I was good to write C++ code without understanding different C++ Initialization types.

So I want to ask, is any practical gain to know and understand all those types? Are there situation where I can write better code or make better decision based on this knowledge? Is there a good reason why a programmer should care about initialization type?

Aucun commentaire:

Enregistrer un commentaire