what is initialization list in c++ code example
Example 1: c++ class member initialization
class Test {
private:
std::string data;
public:
test(std::string data) : data(data) {}
std::string getData() {
return data;
}
};
int main() {
Test test("This is just some random Text");
std::cout << test.getData() << std::endl;
return 0;
}
Example 2: initialization list c++
struct S {
int n;
S(int);
S() : n(7) {}
};
S::S(int x) : n{x} {}
int main() {
S s;
S s2(10);
}
Example 3: c++ class member initializer list
#include <iostream>
class Entity {
private :
std::string m_Name;
int m_Score;
int x, y, z;
public:
Entity()
:m_Name("[Unknown]"),m_Score(0),x(0),y(0),z(0)
{
}
Entity (const std::string& name)
:m_Name(name)
{}
const std::string& GetName() const { return m_Name; };
};
int main()
{
Entity e1;
std::cout << e1.GetName() << std::endl;
Entity e2("Caleb");
std::cout << e2.GetName() << std::endl;
std::cin.get();
}
Example 4: c++ initialization list
class Something
{
private:
int m_value1;
double m_value2;
char m_value3;
public:
Something()
{
m_value1 = 1;
m_value2 = 2.2;
m_value3 = 'c';
}
};