|
Roger Stoltz wrote: Could be. I was thinking about the factory pattern, but the OP said that would always be using new to create the objects. That's why I suggested overriding the new operator.
My recommendation (instead of overriding the new/delete operators) would be to use a smart pointer (since it appears he wants it to self-destruct):
class MyClass<br />
{<br />
MyClass() { cout << "I got created!" << endl; }<br />
~MyClass() { cout << "I got destroyed!" << endl; }<br />
};<br />
<br />
void main()<br />
{<br />
shared_ptr<MyClass> pClass(new MyClass);<br />
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
couldn't be more correct;P
codito ergo sum
|
|
|
|
|
I'm trying to compile my old application in VS2005.
But the compiler no longer accepts the default assignment of NULL to a vector iterator. How do I resolve this?
I declare my function like this:
vector<CSomeClass*>::iterator MyFunction(vector<CSomeClass*>::iterator itStartPos = NULL);
The compiler says:
error C2440: 'default argument' : cannot convert from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'
I can't default the parameter to myVectorList.begin() because the function declaration is not aware of which list the iterator belongs.
Thanks
--
The Obliterator
-- modified at 7:40 Friday 2nd June, 2006
|
|
|
|
|
Obliterator wrote: function declaration is not aware of which list the iterator belongs.
does the function itself know ? if not, how do you know the iterator is valid ? (can't test against vec.end() if you don't have the vector)
a quick run through Google tells me that it's not possible, in general, to set iterators to NULL. iterators are not pointers, for one thing.
i think you'll need to re-work that function a bit
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
Good point. Actually, I simplified the function declaration for the purposes of the queston. The end vector is also supplied as a parameter to the function.
However, the function is indeed aware of the list and simply defaults to the beginning of the list if itStartPos parameter is NULL.
I guess your right - I'm going to have to drop the default parameter here and update each call to the function. Serves me right for using a hack to assign NULL to it in the first place. Damn this new strict checking!!!
Thanks for your response. Much appreciated.
--
The Obliterator
|
|
|
|
|
Starting with VC7, STL iterators were type-checked better. You can't treat a collection<T>::iterator as a T* (or any other pointer value, like NULL).
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
So I see. This is causing me some real headaches!!!
Is there no assignement that can be made to the iterator which means its invalid?
I used to assign NULL to iterators to mean they wern't being used.
Then I could simply check its NULL status and act accordingly.
There must be a way of saying an iterator is invalid.
If I assign it the value of vectorlist.end() I could test against that - but does initial end assignment remain valid after things are added to the vector list?
--
The Obliterator
|
|
|
|
|
CSocket * sock;
is it a right way to delete a pointer
sock = NULL;
delete sock;
Or can I directly do
delete sock;
Regards.
|
|
|
|
|
zahid_ash wrote: sock = NULL;
delete sock;
This won't do anything !
You first have to delete the pointer THEN set it to NULL:
delete sock;
sock = NULL;
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
CSocket* pSocket;
// Allocate a memory for pSocket
// Delete a allocated memory
if(pSocket)
{
delete pSocket;
pSocket = NULL;
}
Regards
Amar.
|
|
|
|
|
I think you can even skip the checking for NULL :
delete pSocket;
pSocket = NULL;
This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL .
-- modified at 9:42 Friday 2nd June, 2006
|
|
|
|
|
Viorel Bejan wrote: This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL.
This only happens in debug builds. You should ALWAYS check for null before deleting a pointer. Calling delete NULL has undefined behavior and should be avoided.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
|
Yes, the standard says that. However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things.
Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Nemanja Trifunovic wrote: Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.
Let me put it this way ...
There is a reason why the DirectX libraries define the following macro:
#define SAFE_DELETE(p) if(p) { delete p; p = NULL; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: There is a reason why the DirectX libraries define the following macro
Yep, there is: Microsoft DirectX programmers don't know C++ very well.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Zac Howland wrote: Calling delete NULL has undefined behavior and should be avoided.
Nope, delete NULL is required by the C++ Standard to do nothing.
It is perfectly safe.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Always NULL the pointer after deleting it. Otherwise you could (if you're not very, very careful) try to delete the same pointer twice, which will result in an access violation.
Deleting a NULL pointer however is perfectly safe.
|
|
|
|
|
Another reason for doing that is that the address used for the recently-deallocated memory may still be valid in your address space and while not technically valid for use, accessing it might not cause an IPF or Access Violation. For example:
TCHAR *pcBuffer = new TCHAR[ 1024 ];
delete [] pcBuffer;
pcBuffer[ 1 ] = _T( 'A' ); The above code may not crash even though the pointer is technically invalid.
By setting it to NULL , you just about guarantee that accessing it will cause an Access Violation (at least if on Win32 and if the access range of the pointer is < 4096 , because that hits the reserved "NULL pointer page" which causes an instant exception, IIRC).
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Can anyone tell me the function call to get the path of the application... i cant for the life of me remember..
Thanks
Lee
|
|
|
|
|
See GetModuleFileName
whitesky
|
|
|
|
|
|
|
The current directory and the "path of the application" are not necessarily the same thing. Most requests are for the latter.
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|