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