-
December 29th, 2008, 04:02 PM
#16
Re: Using delete on an object without new
Originally Posted by Paul McKenzie
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.
-
December 29th, 2008, 04:17 PM
#17
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;
}
-
December 29th, 2008, 11:51 PM
#18
Re: Using delete on an object without new
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.
-
December 30th, 2008, 04:14 PM
#19
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
-
December 10th, 2013, 06:43 AM
#20
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.
-
December 10th, 2013, 08:43 AM
#21
Re: Using delete on an object without new
Originally Posted by exterminator
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.
-
December 10th, 2013, 08:53 AM
#22
Re: Using delete on an object without new
Originally Posted by Gorbo
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.
-
December 10th, 2013, 09:08 AM
#23
Re: Using delete on an object without new
-
December 10th, 2013, 09:10 AM
#24
Re: Using delete on an object without new
Originally Posted by OReubens
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|