0
Why do I need member initializers?What the real importance?
Why I need to initialize the constant variable using member initializers whereas I can initialize it where I declared the constant variable.Likewise the given example: #include <iostream> using namespace std; class MyClass { public: MyClass(int a); private: int regVar; const int constVar=5; }; MyClass::MyClass(int a) { regVar=a; cout << regVar << endl; cout << constVar << endl; } int main() { MyClass obj(42); } Output: 42 5 So,why do I need member initializers,If anyone knows do reply plz.Or it just an alternative way of initializing const variable?
10 Respostas
+ 4
Maybe this can also help:
https://www.geeksforgeeks.org/when-do-we-use-initializer-list-in-c/
+ 2
It's just another way to initialize your members since C++11.
https://en.cppreference.com/w/cpp/language/data_members
Scroll down to 'member initialization', and then the 2nd option.
0
Hadia [ Gone Heartless ] I had done that lesson -_-
0
Hadia [ Gone Heartless ] Have u seen my given example,it is initialized.
0
Hadia [ Gone Heartless ] tnx for co-operation.-,-
0
Dennis tnxs