Re: Using delete on an object without new
Quote:
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??
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;
}
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.
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 :D
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.
Re: Using delete on an object without new
Quote:
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.
Re: Using delete on an object without new
Quote:
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.
Re: Using delete on an object without new
Re: Using delete on an object without new
Quote:
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.