Header and Classes Question

M

mrbluelamp

I have two programs, ProgramA and ProgramB, they both use a common
header, well I am wanting to use a common header, if possible. This is
similiar to what I have, in ProgramA.h I have the code:

#if !defined(ProgramA)
#define ProgramA

The problem is, that it always goes into the #else section, am I
missing something?

//Rules.h

#if !defined(RULESIncluded)
#define RULESIncluded

#ifdef ProgramA
#include "Branch.h"
#include <vector>

class Rules
{
public:
Rules(char *iniFile);
~Rules();
static inline bool IsValid(char* text);
static Data* GetData(const char* name);
private:
static std::vector<Data*> names_vec;
};
#else
class Rules
{
public:
Rules();
~Rules();
static inline bool IsValid(char* text);
};
#endif

bool Rules::IsValid(char* text)
{
//Code Here
return true;
}
#endif
 
M

Mark P

I have two programs, ProgramA and ProgramB, they both use a common
header, well I am wanting to use a common header, if possible. This is
similiar to what I have, in ProgramA.h I have the code:

#if !defined(ProgramA)
#define ProgramA

The problem is, that it always goes into the #else section, am I
missing something?

This seems like an awfully complicated construction. What are you
really trying to accomplish?

When you say it always goes into the #else section I'm guessing you're
referring to the code below. Keep in mind that unless you #include
"Rules.h" _after_ you #define ProgramA, then ProgramA will not be
#defined when Rules.h is input. It would be helpful if you showed us a
complete set of files that exhibits this problem.

It seems like you're trying too hard to use preprocessor directives.
Since the preprocessor is relatively dumb and the compiler is relatively
smart, it's generally a good idea to rely on the preprocessor as little
as possible.

The #ifndef NAME #define NAME ... #endif pattern is usually employed as
an include guard, i.e., a way to make sure that a header doesn't get
included multiple times during the compilation of a single translation
unit (think .cpp file). It's rather less common to use it to make
logical decisions about what the code should look like and I'd advise
against doing so unless you have good reasons. If the class Rules is
different for ProgramA and ProgramB either make two separate headers or
abstract out only the similar parts, but I'd still recommend the former.

Mark
 

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,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top