Connecting Tech Pros Worldwide Help | Site Map

Collection class helper objects in classes?

  #1  
Old July 22nd, 2005, 05:36 PM
Mark Buxbaum
Guest
 
Posts: n/a
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
  #2  
Old July 22nd, 2005, 05:36 PM
Alf P. Steinbach
Guest
 
Posts: n/a

re: Collection class helper objects in classes?


* Mark Buxbaum:[color=blue]
>
> Is is possible to use a collection class instance as a helper
> object in a class?[/color]

Yes.

[color=blue]
> For example:
>
> MyClass.h:
> ----------[/color]

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


[color=blue]
> class MyClass
> {
> map<string, string> m_mapMyMap;[/color]

Here you should qualify with "std::" since this is in a header file.
[color=blue]
> };
>
> MyClass.cpp:
> ------------
>
> #include "stdafx.h"
> #include <string>
> #include <map>
> using namespace std;
> #include "MyClass.h"[/color]

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.

[color=blue]
> "syntax error: missing ';' before '<' in MyClass.h[/color]

That has nothing to do with the code shown.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old July 22nd, 2005, 05:36 PM
David Rubin
Guest
 
Posts: n/a

re: Collection class helper objects in classes?


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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Protected static members, abstract classes, object composition vs. subclassing Kevin Prichard answers 11 November 29th, 2005 01:45 AM
Rectangle: struct or class? Steven T. Hatton answers 15 July 22nd, 2005 07:02 PM
Hypothetical: All code in classes but main() Steven T. Hatton answers 47 July 22nd, 2005 10:38 AM
Hypothetical: All code in classes but main() Steven T. Hatton answers 45 July 22nd, 2005 09:52 AM