Hey everyone
Here's my problem: In my last rewrite of my game, there were memory leaks everywhere. In this rewrite, I don't want that to happen. If it does, tracking them down takes a lot of time.
So here's my objective: Make a class that can track memory leaks.
So I'm thinking, I'll just make a templated class that will tell me if the data wasn't freed by the time the destructor fires. Here's what I have so far:
The last function, the operator Type*() one, is to allow the use of dereferencing and the -> operator. Dereferencing works beautifully. The -> operator doesn't. So I looked on google, it turns out overloading the -> operator isn't allowed. But I'm not overloading it, I'm doing a typecasting... thing. (Not sure how to put that in words, lol.)Code:template<class Type> class vDynamicData { private: Type* Data; char* Name; public: vDynamicData( char* _Name ); ~vDynamicData(); void Allocate(); void Free(); Type* Get(); operator Type*(); }; template<class Type> vDynamicData<Type>::vDynamicData( char* _Name ) { Data = NULL; Name = _Name; } template<class Type> vDynamicData<Type>::~vDynamicData() { if( Data ) { cout << "Unfreed: " << Name << endl; } } template<class Type> void vDynamicData<Type>::Allocate() { Data = new Type; } template<class Type> void vDynamicData<Type>::Free() { delete Data; Data = NULL; } template<class Type> vDynamicData<Type>::operator Type*() { return Data; }
So how can I get the -> operator to work?
Here's the code where I test it out:
and here's the errors I get:Code:struct MyType { int A; bool B; char C; }; int main() { vDynamicData<MyType> Num( "Test MyType" ); Num.Allocate(); Num->A = 3; Num->B = true; Num->C = '@'; cout << Num->A << Num->B << Num->C << endl; Num.Free(); return 0; }
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(67): error C2227: left of '->A' must point to class/struct/union
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(71): error C2227: left of '->A' must point to class/struct/union
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(68): error C2227: left of '->B' must point to class/struct/union
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(71): error C2227: left of '->B' must point to class/struct/union
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(69): error C2227: left of '->C' must point to class/struct/union
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(71): error C2227: left of '->C' must point to class/struct/union
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(67): error C2819: type 'vDynamicData<Type>' does not have an overloaded member 'operator ->'
with
[
Type=MyType
]
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(68): error C2819: type 'vDynamicData<Type>' does not have an overloaded member 'operator ->'
with
[
Type=MyType
]
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(69): error C2819: type 'vDynamicData<Type>' does not have an overloaded member 'operator ->'
with
[
Type=MyType
]
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(71): error C2819: type 'vDynamicData<Type>' does not have an overloaded member 'operator ->'
with
[
Type=MyType
]
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(71): error C2819: type 'vDynamicData<Type>' does not have an overloaded member 'operator ->'
with
[
Type=MyType
]
c:\Documents and Settings\Evan Ovadia\My Documents\Visual Studio Projects\vDynamics\Main.cpp(71): error C2819: type 'vDynamicData<Type>' does not have an overloaded member 'operator ->'
with
[
Type=MyType
]