typedefs, #defines

Started by
6 comments, last by Dirk Gregorius 19 years, 10 months ago
I have some classes that I like to compile into a core lib so I can use them easily in upcoming projects. My idea now is to typedef common types like e.g. Byte, Word, DWord, etc but also something like Index16 and Index32. Some of these types may be already defined in other headers ( windows.h for example ). What is the common strategy to do this? Do I have to keep some special including order? This is my idea at the moment: I have several classes in my core module ( one .h and .cpp file for each class ). Then I define one master header ( the only public header for this module ) let's call it core.h using the following structure:

// First we define/typedef the basic types which is done 

// platform dependent in seperate headers

#ifdef _WIN32
#include <Win32Types.h>
#ifdef SOMETHING_ELSE
...
#endif

// Define anything else


// Then I include the class header of the module

#include <Vector.h>
#include <Matrix.h>
#include <Archive.h>
All comments are welcome!! Best regards -Dirk [edited by - DonDickieD on June 10, 2004 6:28:56 AM] [edited by - DonDickieD on June 10, 2004 6:29:34 AM]
Advertisement
look inte c++ namespaces, they were designed to overcome this problem (namespace ''pollution'')

first google link: http://www.glenmccl.com/ns_comp.htm
dunno how good it is :-)

Good Luck!

Willem

Hello DonDickieD,

use namespaces.
If your are creating a library to use in multiple projects then namespace is the way to go.

you can access typedef, external variables, classes etc. in a namespace by two ways.

In a header use (name of namespace):: (var name, typedef etc.)
In a source file you can do using namespace (name of namespace).

you can have many using name spaces if you want.
do not put using namespace in header files.

ex.

in my_vector.h

namespace myspace {
declearation of typedef, classes, variable etc.
}
header

#include <vector>
#include <my_vector>

std::vector std_vect;
myspace::vector my_space_vect;
myspace::Int16 int16_from_myspace;

in source

using namespace std;
using namespace myspace;

then you can just use

Int16 this_came_form_myspace;

if compiler says there a conflict you can still always use
myspace::Int16 this_came_form_myspace;

Hope this helps.

Lord Bart

[edited by - lord bart on June 10, 2004 8:41:29 AM]
Obviously, this doesn''t work for defines!

you can always guard them with

#ifdef

#endif

look at #undef
Namespaces are the way to go - unfortunately some librarys like win32 create rather a lot of annoying #defines - I guess you could #undef ones which annoy you (provided you don''t want to use them).

But you thought win32 was bad? Try curses, it''s even worse

Mark
Thanx everybody,

namespace seem the way to go.

Is there anything that speeks against having a header where I define my types like this:

Types.h
namespace core{typedef....}


And then I make sure the header is simply included everywhere it is needed.

-Dirk
Hello DonDickieD,

You got the right idea.
That is the way it is done.

Lord Bart

This topic is closed to new replies.

Advertisement