I have returned the pointee type from operator T*(), and invoked delete operator over smart pointer, and tried invoking the member function, and i have not got any run time error. How is that possible? Or is my understanding is wrong? Kindly suggest.
#include <iostream>
using namespace std;
template <typename T>
class sPtr
{
private:
T * pointee__;
public:
operator T * () {
cout <<"Inside T* () "<<endl;
return pointee__;
};
explicit sPtr ( T * t )
{
pointee__ = t;
};
T * operator->() {
return pointee__;
}
};
class JTest
{
private:
int x;
public:
JTest ( int l=100) { x=l; };
void display ();
};
void JTest::display()
{
cout <<"Display API x is "<<x<<endl;
}
void JFunc (JTest * tmp)
{
cout <<" JFunc"<<endl;
tmp->display ();
cout <<"Invoking JTest -> display "<<endl;
}
int main ( int argc, char ** argv)
{
sPtr <JTest> t(new JTest);
t->display();
delete t; // Invokes operator T* () , and it is dangerous!!!..
t->display ();
}
OUTPUT:
Display API x is 100
Inside T* ()
Display API x is 100