Skip to content

Instantly share code, notes, and snippets.

@Adobe-Android
Last active July 10, 2020 14:31
Show Gist options
  • Save Adobe-Android/92dc95282302da727be2e757c6ba3e2e to your computer and use it in GitHub Desktop.
Save Adobe-Android/92dc95282302da727be2e757c6ba3e2e to your computer and use it in GitHub Desktop.
User defined objects and how they can be used
#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