@jsmith "Tis an issignment, for pedagogical reasons i guess, maybe his or her teacher already knows boost ant the auto_ptr thing!!!
Ok, since it's about tring to clarify things let's get to it. Like this it's no way a safe pointer. Consider:
1 2 3 4 5
|
puntero<int> int_ptr(new int(0) );
int int_var = 100;
int_ptr = &int_var; // ouch, what u are trying to avoid actually happens
|
The thing is that ur assignement operator in fact made ur pointer point at another variable on the stack but it never went outta scope so the desctructor to realease the resourse on the heap was never called, memory leak. You have to call the destructor from the assingment operator to avoid that, would ne something like this:
1 2 3 4 5 6 7
|
// it doesnt make much a diferen it return void in this case in fact u might find some problems returning "this"...
void operator =(type * tp)
{
this->~puntero(); // free memory before new assigments...
p = tp;
}
|
Now we have another problem, when the pointer actually goes outta scope will call the destructor again!!! Trying to release memory on the heap that doesnt exist if actually the second assinment was to a variable on the stack, nasty thing!!!! You have to make yr destructor aware od such things with something like this maybe:
1 2 3 4 5 6 7
|
~puntero()
{
static int guard = 0;
if(p && !guard) delete p;
p = 0;
guard++;
}
|
You have to specify for the users this:
The restriction is to create a dynamic variable only the first time then safely assign to another variable on the stack, NOT ANOTHER DYNAMIC VARIABLE! But then why would u want to do that??!!
That's the kind of things that happens trying to tame pointers!
To print the adress of ur pointer well Jsmith already told u how!
But now, what if i want to do this??
1 2
|
puntero<int> *iptr;
// add this feature
|
With respect to keeping the window open i made myself this:
1 2 3 4 5 6 7 8 9
|
inline void pause_scr()
{
std::cout << "Press <ENTER> To Quit!" << std::endl;
std::cin.ignore( std::cin.rdbuf()->in_avail() +1 );
}
// No need for extra "includes"!!!!!!
|
There is a way to print the line but it's a lot of work trust me...
Good luck!