Best cpp object destruction ever



  • class Request
    {
    public :
    int type;
    std::string RequestSerialized;
    };

    class HandleRequest : public Request
    {
    public:    
        std::vector<std::string> reqDataVectSpecial;
        std::string name;
    	
        HandleRequest()
        {
            type = 1;
        }
    };
    
    class SetRequest : public Request
    {
    public:
        SetRequest()
        {
            type = 2;
        }
    };
    
    Request* req = (Request*)m_messageRequests[it->second];
    switch (req->type)
    {
    case 1:
    	delete (HandleRequest*)req;
    	break;
    case 2:
    	delete (SetRequest*)req;
    	break;
    default:
    	delete req;
    	break;
    }
    req = NULL;


  • It scares me that they think C++ doesn't internally know what the runtime type of a variable is.



  • I haven't touched c++ in many years, but I think this is the correct way to do it if you don't have a virtual destructor.

    But the correct thing to do, is to add a virtual destructor, not trying to implement a virtual destructor by hand.



  • Did the author ever heard of virtual functions? Or a virtual destructor? Or does he work in Samsung, which would explain it.



  • It knows if you have at least one virtual function (technically a virtual table + RTTI enabled).



  • Are these idiots compiling with -fno-rtti out of cargo cult thinking?

    (There are projects that use that switch legitimately, but it's something that you must think through before reinventing yourself.)



  • @tarunik said:

    Are these idiots compiling with -fno-rtti out of cargo cult thinking?

    (There are projects that use that switch legitimately, but it's something that you must think through before reinventing yourself.)

    You won't need RTTI for virtual destructors. The destructor becomes part of the vtable.



  • @Evo said:

    You won't need RTTI for virtual destructors. The destructor becomes part of the vtable.

    Yeah, wouldn't surprise me if basic (single inheritance) virtual dispatch was implemented without having to delegate to the RTTI mechanism post-construction...


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.