My C++ is pretty rusty so now that I started using it for a hobby project I got to “level up”-again..
#include "stdafx.h"
#include "stdlib.h"
class a
{
public:
void call() { printf("CALL called\n"); }
};
class b
{
public:
b() { this->pointer = new a; }
void call() { this->pointer->call(); }
private:
a* pointer;
};
int _tmain(int argc, _TCHAR* argv[])
{
b t;
t.call();
system("PAUSE");
return 0;
}
Will that result in a memory leak? And how can I delete the pointers if the program decides it does not need them anymore?
Would “delete t” be enough or would that produces a memory leak too?
pointerinbis allocated but never deleted. You’ll want to define a destructor forbthat deletesa, otherwise you will leak theapointed to bypointerevery time abgoes out of scope: