Is it usual to rely completly on the new operator in constructors/copy constructors. What if new trows an exception? The application ends and that's it? The new operator can be placed where it can't be catch like in constructor initialization list. What kind of practice I should adopt when using "new" in those cases?
Unique_ptr handles ownership of a pointer, but I think you still need new to allocate the pointed-at object in the first place.
If you have a unique_ptr to an object and an exception gets thrown (such as when you're out of memory), the pointed-at object will get deleted if the unique_ptr gets deleted. Of course, if the destructor does anything to try to allocate memory, or calls something that tries to allocate memory, then that code is likely to throw an exception too.....
template <class T>
class WndMessageMaps
{
private:
struct MsgProc { UINT uMsg; T pProc; };
std::vector<MsgProc> table_;
INT index_;
public:
// constructor
WndMessageMaps(INT nElements = 8) :
table_(nElements),
index_{ 0 }
{
}
T find(UINT uMsg)
{
for (int i = 0; i < index_; ++i)
{
if (table_[i].uMsg == uMsg)
{
return table_[i].pProc;
}
}
return NULL;
}
// other stuff below
}
By the way: it's generally not a good idea to just blindly hope that your class is instantiated with a pointer type or a type that can have a null state - instead expect the type itself and transform it within the class.
By the way: it's generally not a good idea to just blindly hope that your class is instantiated with a pointer type or a type that can have a null state - instead expect the type itself and transform it within the class.
I don't understand what you mean. Can you give me an example so I can figure it out. Thanks.
I see. But expect one or the other this is still "expect ".
In the case I posted above it is a message map that route the message to the proper window/dialog proc and Windows defines them as below...
So I'm "expecting" DLGPROC or WNDPROC. In fact it's not true because I've defined my own window proc and by the way this class is only used in another class in a "has-a" relationship. That's another story.
To go back to the main subject, as you posted in your code that imply the vector class, I see it all the time and from all I've read, the overhead seem very low (or almost null) compared to C-array. And in your code I could remove the index_ because I think that the vector keep all that is needed to parse the array (size method). In fact I didn't verify what it would look like using it with the find method but I'll see.