30 lines
590 B
C++
30 lines
590 B
C++
// $ g++ -o raii raii.cpp
|
|
// https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization
|
|
#include <cstdio>
|
|
class MyClass {
|
|
int *n;
|
|
public:
|
|
MyClass(int x)
|
|
{
|
|
this->n = new int[x];
|
|
printf("constructor\n");
|
|
}
|
|
~MyClass()
|
|
{
|
|
delete this->n;
|
|
printf("deconstructor\n");
|
|
}
|
|
int* get_n()
|
|
{
|
|
return this->n;
|
|
}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
MyClass *b = new MyClass(5);
|
|
int *xs = b->get_n();
|
|
printf("%d\n", xs[0]);
|
|
delete b;
|
|
printf("%d\n", xs[0]);
|
|
} |