Class Heap And Stack Initialisation
Given the following class
#pragma once
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include "./baseEntity.h"
template<typename T>
class Entity : public BaseEntity {
int id;
T value;
public:
Entity();
Entity(T value);
void info() const override;
};
#include "entity.tpp"
#include "../db/entity.h"
#include <string>
class Book: public Entity<std::string> {
public:
Book(std::string name): Entity<std::string>(name){
};
};
For heap initialisation, we have 2 ways
Stack initialisation
For stack initialisation, memory might be overwritten at any point
Book book("C++ 20 Reference");
Next however the pointer of this book (&book
) might not persist and lose
Heap initialisation
If you prefer to use heap, we can do one with new
Book* book = new Book("C++ 20 Reference");
book
now will be the pointer reference and will persist through the program