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? 4 7644 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 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!" 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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: A |
last post by:
Hi,
Consider this code:
//Header File - Foo.h
int i = 0; // non-static global variable
class Foo{
...
|
by: Santiago de Compostela |
last post by:
Hi
The following program doesn't compile on MS VC++ or Bloodshed Dev-C++
#include <iostream>
int strlen(const char *in)
{
|
by: bob |
last post by:
Stroustrup 3rd edition, 4.9.4, 2nd paragraph:
"A name is called global if it is declared outside any function, class
(Chapter 10), or namespace. The scope of a global name extends from the
point...
|
by: Jay Wolfe |
last post by:
Hello,
I'm trying to make sure I use best practices (and hence save myself some
headaches) with the declaration and definition of global variables. Let's
say I have an app with 30 files,...
|
by: Spiros Bousbouras |
last post by:
Let's say I have a global variable int var which I want
to be known to translation units T1 and T2, I want T1
to be able to read its value and modify it and T2 to be
able to read its value but not...
|
by: yinglcs |
last post by:
Hi,
I read this article about global variable in c:
http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html
But I have a few questions
1. how can I declare the global variable...
|
by: viraj.kadakia |
last post by:
I observe a behavior with shared libraries (.so) and global variables
that I cannot understand ...
I'd appreciate if someone can explain the behavior ..
Scenario 1:
aTest is an executable on...
|
by: Saeed Amrollahi |
last post by:
Dear all
Hi
I am Saeed Amrollahi. I write C++ programs using VC++ 2005 CLR/CLI. I
have two problems:
1. How to declare/define and use global ref class objects? For example
for database...
|
by: Jaco Naude |
last post by:
Hi,
I'm using a static library in my application which links fine except
for a few global variables. The static library only contains a bunch
of .cpp and .h files and the global variables are...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |