markbuxbaum@hotmail.com (Mark Buxbaum) wrote in message news:<fb0e106.0407171704.50e52df8@posting.google.c om>...[color=blue]
> Hi,
>
> Is is possible to use a collection class instance as a helper object in a class?[/color]
s/collection/container/
[color=blue]
> For example:
>
> MyClass.h:
> ----------
>[/color]
You should
#ifndef INCLUDED_MAP
#include <map>
#define INCLUDED_MAP
#endif
#ifndef INCLUDED_STRING
#include <string>
#define INCLUDED_STRING
#endif
here
[color=blue]
> class MyClass
> {
> map<string, string> m_mapMyMap;[/color]
This should be
std::map<std::string, std::string> m_mapMyMap;
You *do* *not* want to have a 'using namespace std' statement in your
header file. This is extremely nasty for clients of your 'MyClass'
component who might define types or variables such as 'string' and
'map' (although this is bad practice on their part).
Also, the name 'm_mapMyMap' leaves something to be desired. The 'map'
prefix is redundant and binds your implementation to std::map. If you
change the implementation, you shouldn't have to go through the
trouble of changing the variable name too. Typically, you would pick a
more descriptive name such as 'm_registry' or 'm_dictionary', and give
a comment explaining the data member's use. (I realize you might have
been intentionally generic in your post, but I mention this anyway for
everyone's benefit.)
[color=blue]
> };
>
> MyClass.cpp:
> ------------[/color]
You *must*
#include "MyClass.h"
as the first included file. This ensures that 'MyClass' is an entirely
self-contained component. That is, clients don't need "special
knowledge" to use it (such as they must also include string and map,
which are really implementation details).
[color=blue]
> #include "stdafx.h"
> #include <string>
> #include <map>[/color]
You don't need to include string and map again, although it doesn't
hurt (except slightly in compile time since you must find and open
each of these files).
[color=blue]
> using namespace std;
> #include "MyClass.h"[/color]
IMO, 'using namespace std' is bad practice, even in an implementation
file. YMMV.
/david