Last active
September 17, 2020 17:25
-
-
Save Adobe-Android/f0955b46511bc0bc7464c9d4fcdb127d to your computer and use it in GitHub Desktop.
A demonstration of the advantages of using member initializer lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using string = std::string; | |
struct Example { | |
public: | |
// Default constructor | |
Example() { std::cout << "Created Entity!\n"; }; | |
Example(int x) { std::cout << "Created Entity with " << x << "!\n"; } | |
}; | |
struct Entity { | |
private: | |
string m_Name; | |
Example m_Example; | |
public: | |
// Member initializer list | |
Entity() : m_Name("Unknown"), m_Example(8){}; | |
// Entity() { | |
// m_Name = "Unknown"; | |
// m_Example = Example(8); | |
// } | |
}; | |
int main() { | |
Entity e; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment