Skip to content

Instantly share code, notes, and snippets.

@Adobe-Android
Last active September 17, 2020 17:25
Show Gist options
  • Save Adobe-Android/f0955b46511bc0bc7464c9d4fcdb127d to your computer and use it in GitHub Desktop.
Save Adobe-Android/f0955b46511bc0bc7464c9d4fcdb127d to your computer and use it in GitHub Desktop.
A demonstration of the advantages of using member initializer lists
#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