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

global instances and extern help

P: n/a
Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif

So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */
#endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project. However, I found it fail to compile the following code

// main.hpp
#include "global.hpp"

int main(void)
{
dc.show();
return 0;
}

Any idea?

Dec 27 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
wa***@wakun.com wrote:
Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif

So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */
#endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project. However, I found it fail to compile the following code

// main.hpp
#include "global.hpp"

int main(void)
{
dc.show();
return 0;
}

Any idea?

Hi,
Could you please show us the compiler error?
Regards,
Peter Jansson
Dec 27 '05 #2

P: n/a
wa***@wakun.com wrote:
Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif

So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */ extern DC dc; /* Why are you initializing in a declaration? */ #endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project. However, I found it fail to compile the following code

// main.hpp
#include "global.hpp"

int main(void)
{
dc.show();
return 0;
}

Any idea?

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 27 '05 #3

P: n/a
wa***@wakun.com wrote:
Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DS.h"

extern int data;
extern DS stru; /* DS is a struct definied in DS.h */
#endif

/* global.c */
#include "DS.h"
#include "global.h"

int data;
DS stru;
#endif
What is this last #endif for?
So, I only need to include global.h when and where I need to access
data and stru. Moreover, though the whole session, only one instance
of data and stru is kept.

Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "DC.hpp"

extern int data;
extern DC dc(2); /* DC is a class definied in DC.hpp */
#endif

// global.cpp
#include "DC.hpp"
#include "global.hpp"

int data;
DC dc(2);
#endif
Again, what is this last #endif for?

Note that dc take an integer as parameter.

I am intending to keep one instance of DC(i.e. dc) in the whole
project.


Try to learn about Singleton, a Design Pattern.
Cheers

--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 27 '05 #4

P: n/a
wa***@wakun.com writes:
Hi there,
I have been using C for years. For some project I've done times ago,
I always need to share variable between modules. The structure of my
project is illustrated as follow

/* global.h */
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
Anything following this line is undefined behavior. Identifiers starting
with an underscore followed by an uppercase letter is reserved for use
by the implementation.

[ snip ]
Now, I have changed to use C++ instead and class is involved.
Similarly, I organize my project as follow

// global.hpp
#ifndef _GLOBAL_H_
#define _GLOBAL_H_


Likewise.
I suggest you use a pattern for your include guards similar to:
H_FILENAME

If you always start with H_ you also avoid the problem with
reserved identifiers starting with an uppercase E. Also avoid
double underscores everywhere.

A simple rule of thumb, which exclude some legal names, but don't
include any reserved names is (except those of existing functions
and macros defined by the standard library):

Don't start any identifer with underscore, uppercase E, str or mem,
and don't use double underscores anywhere in your identifiers.

If you really want to use names starting with str or mem, append
an underscore (i.e. str_ and mem_).

/Niklas Norrthon

Dec 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.