Jump to content
  • Advertisement

Archived

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

Dirk Gregorius

typedefs, #defines

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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.
}
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]

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!