Hi,
I need a special class X to make this work without errors
so what am i looking for besides overloading delete?
void main()
{
X x;
delete x;
}
thanks
Printable View
Hi,
I need a special class X to make this work without errors
so what am i looking for besides overloading delete?
void main()
{
X x;
delete x;
}
thanks
Why? What purpose does instantiating an object, and then calling "delete" serve?
Also, main() returns int, not void.
That is the only way to do what you want. The delete requires that the argument is a pointer. Since it must be a pointer, the X is a typedef for a pointer to some real class. Also, I had to set it to 0, so that issuing the delete call doesn't lead to undefined behaviour.Code:struct SomeX
{
};
typedef SomeX* X;
int main()
{
X x;
x = 0;
delete x;
}
Regards,
Paul McKenzie
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). So, I opted to change the naked pointer usage with a shared_ptr with the deleter set to a noop routine which I used in case the pointer was to the stack object else it had the default deleter as delete in cases it were pointing to the free store. But, I did not need to use delete explicitly as the OP is requesting for. So, not sure what he actually needs that for.
So main is void instead of int big deal it's not a real program just a test program and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...
anyway i hope i cleared things up and you can point me to the right direction now...
You must overload operator new() before overload operator delete().
Test programs are real programs.
You're responding to professional programmers who have decades of experience here -- don't expect that we won't ask you questions on what you're really trying to do.Quote:
and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...
Operator delete requires a pointer. That class instance is not a pointer. Therefore there is no way you can get that code to compile unless X is a pointer -- that's why the typedef was needed.
Unless you can change the rules of the C++ language, you're stuck. And because you're stuck is indeed the reason why we want to know what you're really trying to accomplish.
Regards,
Paul McKenzie
Just as an FYI....
I know a decent number of instructors (including secondary school teachers, and university procfessors) who will (correctly) FAIL and assignment with main returning a void.
When I give training courses (and I have a course specifically for teachers) I stringly encourage them to adopt this attitude.
A peice of code which has main returning a void, is NOT a valid C++ program. PERIOD.
Lets try this again
i do not know the purpose of this program i do not know what i am trying to accomplish
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!
now if you know how to accomplish this please respond if not don't bother.
Sounds like a useless assignment from a misguided teacher. Anyway, Paul McKenzie's suggestion of a typedef should work, if by "work" you mean "compile". It may or may not run "correctly" since there would be undefined behaviour, but I am not sure if you can actually avoid that without changing the main function itself.Quote:
Originally Posted by Gorbo
What you're asking makes no sense as many of us have coded for decades without the need and it's not defined in the language. You don't know why you need to do it, so the only real logical conclusion is you have a serious design flaw or you don't understand the requirements.
Stack allocated objects get deleted when they go out of scope. You just need to define the scope appropriately so that the objects exist only as long as they're needed.
Objects allocated on the heap using new exist until they're explicitly deleted, which is why you need to delete them.
What you're asking shows that you, or whoever is asking you to do this, lacks an understanding of how C++ and its memory management works. You can keep asking and keep getting frustrated, but it won't change the fact that it just doesn't work that way, and if you understood how it does work, you'd realize what you're asking for makes no sense.
From what Gorbo has stated, I believe the problem is with the requirements. The assignment question is fundamentally flawed.Quote:
Originally Posted by GCDEF
Speaking of changing the rules of the language, another "creative" way of solving the assignment question besides using a typedef might be:
I cannot seem to tell from the C++ standard if redefining delete in this way is not allowed, results in undefined behaviour, or is actually okay, but seeing that the assignment is just wrong one might as well submit this as an "answer".Code:#define delete ;
class X{};
int main()
{
X x;
delete x;
}
So you didn't have any idea that what they are asking you to do is designed to waste your time? That's what it seems like to me, because you cannot do what you say you want to do. It's like asking us to divide by zero and give you a numerical answer.
Again, and this is the last time, there is no way to delete something that isn't a pointer value, so no one can tell you how to accomplish it. What we can do is explain why it cannot be done, which we have painstakingly done, but you refuse to listen.Quote:
now if you know how to accomplish this please respond if not don't bother.
Regards,
Paul McKenzie
I don't have a copy of the standard on me, but I can tell you that that's UB.
Another solution:
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.Code:struct X
{
operator X*() const {return 0;}
};
int main()
{
X x;
delete x;
}
Well this could be what he's after, but delete on a stack based object has to be undefined behaviour I think.
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??Code:class X
{
public:
operator X*()
{
returnthis;
}
};
int main()
{
X x;
delete x;
}
maybe
Code:class X
{
int* wuahaha;
public:
X()
:wuahaha(new int)
{}
operator int*()
{
return wuahaha;
}
};
int main()
{
X x;
delete x;
}
Ah yes, I think your idea "works" best.Quote:
Originally Posted by Plasmator
Now, hopefully Gorbo can just submit this and move on to more useful things.
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
I encountered the same problem.
You do not need to call delete.
As soon as the scope of x ends, ~X() is called.
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.
Thread is from 2008.