0

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
Whoami
  • 13,930
  • 19
  • 84
  • 140
  • 8
    [Stop stealing hotel room keys!](http://stackoverflow.com/a/6445794/46642) – R. Martinho Fernandes Feb 21 '13 at 14:15
  • If your question is: "Why doesn't UB crash on me every time I invoke it", then the answer is: "Because its UB" – PlasmaHH Feb 21 '13 at 14:16
  • You're doing something you're not allowed to do. There's no point asking about what to expect. – Kerrek SB Feb 21 '13 at 14:22
  • 1
    The sign said the speed limit was 30 mph. I tried, and I could go over 60 mph. How is that possible? I was expecting to get arrested. – molbdnilo Feb 21 '13 at 14:34
  • If it were to crash *every* time you invoke and undefined behavior, it would have been *defined* behavior. –  Feb 21 '13 at 14:39
  • I'm not sure that program will produce the output shown. – Watusimoto Feb 21 '13 at 15:07
  • @KerrekSB, I am a less experience in c++ and learning now. If you kindly explain me the reason why it is being closed, it could be helpful for me in feature not to ask such questions in stackoverflow.com. I even did not know that "You're doing something you're not allowed to do' as per your suggestion. Is this forum not for the freshers?. – Whoami Feb 21 '13 at 16:25
  • @Whoami: I think you're generally expected to a) know a bit about how this site works (e.g. *StackOverflow is not a forum*, and "your question has to be useful for *others*"), b) do some research of your own (this point has been discussed *so many* times already), and know the basics of your programming language. It is definitely *not* a tutorial site, or maybe "for the freshers" as you put it. It's OK to be inexperienced, but you have to make an effort. This question is probably closed because there are now useful links to better questions. No hard feelings :-) – Kerrek SB Feb 21 '13 at 17:29
  • 1
    I think it's a reasonable question; it is clear and specific. The questioner saw unexpected behaviour and asked for an explanation. I would not have closed it, especially not for the reason stated. – Watusimoto Feb 22 '13 at 09:23

1 Answers1

3

Deleting t marks the memory pointed to by t as unused, but leaves the address stored in t intact. When you try to use the object stored at t, C++ does not check if it has been deleted, so while what you have will often crash, it may occasionally work if the memory used by the deleted object has not yet been overwritten by the system.

In short: sometimes this will work, often it will crash, and it is always a bad idea.

If you want to protect yourself from yourself, set t to NULL after you have deleted it.

Watusimoto
  • 1,773
  • 1
  • 23
  • 38
  • 2
    The best way to protect yourself from yourself is to not leave any deleted pointers exposed, i.e., make all uses of `delete` be with private data members and always inside destructors. Or better, forget about `delete` and follow the [rule of zero](http://flamingdangerzone.com/cxx11/2012/08/15/rule-of-zero.html). (IOW, if you have a concierge for each customer that always opens their room for them, people cannot steal hotel room keys) – R. Martinho Fernandes Feb 21 '13 at 14:26
  • Thanks Martinho. YOu helped with information, rather... ;). – Whoami Feb 21 '13 at 16:36