Using delete on an object without new - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: Using delete on an object without new

  1. #16
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Using delete on an object without new

    Quote Originally Posted by Paul McKenzie View Post
    there is no way to delete something that isn't a pointer value
    Well this could be what he's after, but delete on a stack based object has to be undefined behaviour I think.
    Code:
    class X
    {
    public:
    operator X*()
    {
    returnthis;
    }
    };
    int main()
    {
    X x;
    delete x;
    }
    
    PS:- How comes copy/paste from my msvc9 express ruins all indentation. Ive set to use spaces instead of tabs like on all previous versions but my code never cut/pastes properly??
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  2. #17
    Join Date
    Nov 2008
    Location
    Netherlands
    Posts
    77

    Re: Using delete on an object without new

    maybe

    Code:
    class X
    {
    int* wuahaha;
    public:
    X()
    :wuahaha(new int)
    {}
    operator int*()
    {
    return wuahaha;
    }
    };
    int main()
    {
    X x;
    delete x;
    }

  3. #18
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: Using delete on an object without new

    Quote Originally Posted by Plasmator
    Like in the typedef example demonstrated by Paul, the invocation of scalar/vector operator delete on a null pointer is guaranteed to be a logical no-op.
    Ah yes, I think your idea "works" best.

    Now, hopefully Gorbo can just submit this and move on to more useful things.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #19
    Join Date
    Oct 2008
    Posts
    45

    Re: Using delete on an object without new

    Ok based on what laserlight and Plasmator wrote i made something similar to the requirements so thanks everyone and especially laserlight and Plasmator

    cya
    For now

  5. #20
    Join Date
    Dec 2013
    Posts
    1

    Re: Using delete on an object without new

    I encountered the same problem.

    You do not need to call delete.
    As soon as the scope of x ends, ~X() is called.

  6. #21
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,782

    Re: Using delete on an object without new

    Quote Originally Posted by exterminator View Post
    One ocassion where in I wanted something like that was when I had 2 code paths and each led to either a pointer to an object on the stack or the free-store and it was a tough to find out when it was pointing to what and correspondingly choose to call delete or not (without adding a state).
    you're only getting yourself into problems with that type of model.
    It's easy to work around. One method: prevent local copies (make the constructors private) and have a static object creation function.

  7. #22
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,782

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    all i know is i was given this code and was told to make it work by creating my own X class that's it!
    Please tell your instructor/teacher he is an idiot.
    Tell him I said so.
    And tell him to come here to come explain in what even remotely realistic setting this is ever going to be an actual need.


    Other than "syntax games" (which really has NOTHING to do with C++ at all) there is no reason to ever do something like this. If this is part of a C++ curriculum other than training in said "syntax games", I stand by my statement he's an idiot.

  8. #23
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: Using delete on an object without new

    Thread is from 2008.

  9. #24
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,272

    Re: Using delete on an object without new

    Quote Originally Posted by OReubens View Post
    Please tell your instructor/teacher he is an idiot.
    Tell him I said so.
    And tell him to come here to come explain in what even remotely realistic setting this is ever going to be an actual need.


    Other than "syntax games" (which really has NOTHING to do with C++ at all) there is no reason to ever do something like this. If this is part of a C++ curriculum other than training in said "syntax games", I stand by my statement he's an idiot.
    Hopefully, after 5 years the instructor has moved on and out of programming.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center