By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

Header and Classes Question

P: n/a
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

Nov 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
mr********@yahoo.com wrote:
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

//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

Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.