MFC Feature Pack includes TR1 which has shared_ptrs.
"Nick Schultz" <email@example.com> ha scritto nel messaggio
I planned on creating a ProtocolPacket class that represents an
packet, and contains a vector of dataElements. dataElement is
contains a pointer to the data, its size(in bytes) and a char*
its field name.
I would need more details, but in general I would say that in
prefer using std::vector as container (instead of raw pointer),
std::wstring or some other string class instead of char*.
Moreover, there is a usual naming convention in C++, that class
start with an upper-case letter (so, I would use DataElement
Lower-case tends to be used for other cases, like class
// Instantiate a DataElement
So, I would define a class or a struct like this:
std::vector< BYTE > Data;
// You don't need a size-in-bytes field here,
// because vector has a size() method for
// that purpose.
// So Data.size() gives you that size.
// I assume that your "field names" here are ANSI only.
// For Unicode, you may use std::wstring.
Then I would store all these DataElement's in a vector like
typedef std::vector< DataElement * > DataElementList;
Note that the vector stores *pointers* to DataElement instances.
If these pointers have a shared ownership semantic, I would wrap
smart pointer like shared_ptr.
typedef boost::shared_ptr< DataElement > DataElementSP;
typedef std::vector< DataElementSP > DataElementList;
In that way, you don't have to pay attention to DataElement
(the shared_ptr smart pointer stores a reference count, and when
0, the object is automatically deleted).
My original implementation called for malloc'ing the necessary
In C++, you would use new instead of malloc(), or a robust
SomeType * p = new SomeType[ count ];
std::vector< SomeType > v[ count ];
From vector, you can have the pointer to the first element
SomeType * pFirst = &v;
If you use new, you must also delete (sooner or later) your
Instead, vector has a destructor that does cleanup.
Moreover, vector can safely grow its size if necessary (e.g.
.push_back( <new data> ); ), and it's guarded against buffer
(which are security enemy #1).
Instead, using raw new, you may have lots of problems like
index, or index completely out-of-range, corrupting nearby
It's not that you must not use new: you may use new, but you
who will mantain your code) must pay lots more attention, and
less robust, more fragile, thant using a robust C++ container
Note that there are also MFC versions of the classes I mentioned
post: you can use CString to store strings, and CArray template
(AFAIK, MFC has no equivalent of smart pointer like