Last active
July 10, 2020 14:31
-
-
Save Adobe-Android/92dc95282302da727be2e757c6ba3e2e to your computer and use it in GitHub Desktop.
User defined objects and how they can be used
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 Entity { | |
Entity() { m_Name = "Unknown"; } | |
Entity(const string& name) { m_Name = name; } | |
const string& get_name() const { return m_Name; } | |
private: | |
string m_Name; | |
}; | |
int main() { | |
// Stack allocated | |
Entity entity{"David"}; | |
// Heap allocated | |
Entity* e = new Entity{"David"}; | |
std::cout << entity.get_name() << "\n"; | |
std::cout << e->get_name() << "\n"; | |
// Free heap allocated variable | |
delete e; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment