how to use new in cpp code example
Example 1: new keyword in cpp
#include <iostream>
#include <string>
using String = std::string;
class Entity
{
private:
String m_Name;
public:
Entity() : m_Name("Unknown") {}
Entity(const String& name) : m_Name(name) {}
const String& GetName() const {
return m_Name;
};
};
int main() {
int* b = new int;
int* c = new int[50];
Entity* e1 = new Entity;
Entity* e = new Entity[50];
Entity* alloc = (Entity*)malloc(sizeof(Entity));
delete e;
Entity* e3 = new(c) Entity();
}
Example 2: new in c++
#include <iostream>
#include <string>
using String = std::string;
class Entity
{
private:
String m_Name;
public:
Entity() : m_Name("Unknown") {}
Entity(const String& name) : m_Name(name) {}
const String& GetName() const {
return m_Name;
};
};
int main() {
int* b = new int;
int* c = new int[50];
Entity* e1 = new Entity;
Entity* e = new Entity[50];
Entity* alloc = (Entity*)malloc(sizeof(Entity));
delete e;
Entity* e3 = new(c) Entity();