intro-to-zig-talk/c/raii.cpp

33 lines
521 B
C++

#include <iostream>
#include <vector>
class MyClass
{
public:
void* ptr;
MyClass()
{
std::cout << "Allocating 1MB\n";
// Allocate 1MB for no reason:
ptr = malloc(sizeof(1000 * 1000));
}
~MyClass()
{
free(ptr);
std::cout << "Destroyed 1MB\n";
}
};
MyClass myClass;
int main()
{
// Trick question, how many times is it constructed and destroyed?
std::vector<MyClass> vecOfMyClasses;
vec.push_back(myClass);
vec.push_back(myClass);
}