Skip to content

Instantly share code, notes, and snippets.

@Cvetomird91
Last active September 19, 2018 06:01
Show Gist options
  • Save Cvetomird91/0238b9d64042136a70b49885bc379b16 to your computer and use it in GitHub Desktop.
Save Cvetomird91/0238b9d64042136a70b49885bc379b16 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <array>
#include <string>
/*
an example of how to allocate an array of objects on the heap,
how to call the default constructor (the one without arguments),
and how to free that array of objects from memory afterwards
*/
class Obj {
public:
int id;
int age;
std::string name;
Obj(int id, int age, std::string name) : id(id), age(age), name(name) {
}
Obj() : id(0), age(24), name("tozi") {}
~Obj() {
std::cout << "object freed" << std::endl;
}
};
int main() {
std::array<Obj, 100> *ary = new std::array<Obj, 100>();
std::array<Obj*, 101> *ary_new = new std::array<Obj*, 101>();
Obj var = ary->at(5);
Obj *p = new Obj[50]();
Obj *pt = &p[5];
std::cout << pt->name << std::endl;
delete[] p;
delete ary;
delete ary_new;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment