#### Archived

This topic is now archived and is closed to further replies.

# typedefs, #defines

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
look inte c++ namespaces, they were designed to overcome this problem (namespace ''pollution'')

dunno how good it is :-)

Good Luck!

Willem

##### Share on other sites
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.
}

#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]

##### Share on other sites
Obviously, this doesn''t work for defines!

you can always guard them with

#ifdef

#endif

look at #undef

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
Hello DonDickieD,

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

Lord Bart

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633402
• Total Posts
3011670
• ### Who's Online (See full list)

There are no registered users currently online

×