Collection class helper objects in classes?

M

Mark Buxbaum

Hi,

Is is possible to use a collection class instance as a helper object in a class?
For example:

MyClass.h:
----------

class MyClass
{
map<string, string> m_mapMyMap;
};

MyClass.cpp:
------------

#include "stdafx.h"
#include <string>
#include <map>
using namespace std;
#include "MyClass.h"

------------

Visual Studio .NET 2003 says:
"syntax error: missing ';' before '<' in MyClass.h

Moving the definition of m_mapMyMap into MyClass.cpp compiles fine.

However, it would be nice to be able to use these collection classes
as private data members within an owning class
(as I believe is possible with Java and C#, which do not have similar
template functionality).

Thanks for any info,
Mark
 
A

Alf P. Steinbach

* Mark Buxbaum:
Is is possible to use a collection class instance as a helper
object in a class?
Yes.


For example:

MyClass.h:
----------

Here you should #include the headers that [MyClass.h] depends on.


class MyClass
{
map<string, string> m_mapMyMap;

Here you should qualify with "std::" since this is in a header file.
};

MyClass.cpp:
------------

#include "stdafx.h"
#include <string>
#include <map>
using namespace std;
#include "MyClass.h"

As a matter of style that last #include should be the first.

Turn off "precompiled headers" for your project to get standard C++
behavior.

You can turn it on later and adjust things so they work also with
that non-C++ behavior.

"syntax error: missing ';' before '<' in MyClass.h

That has nothing to do with the code shown.
 
D

David Rubin

Hi,

Is is possible to use a collection class instance as a helper object in a class?
s/collection/container/

For example:

MyClass.h:

You should

#ifndef INCLUDED_MAP
#include <map>
#define INCLUDED_MAP
#endif

#ifndef INCLUDED_STRING
#include <string>
#define INCLUDED_STRING
#endif

here
class MyClass
{
map<string, string> m_mapMyMap;

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.)
};

MyClass.cpp:
------------

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).
#include "stdafx.h"
#include <string>
#include <map>

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).
using namespace std;
#include "MyClass.h"

IMO, 'using namespace std' is bad practice, even in an implementation
file. YMMV.

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top