473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

problem with declare global variable

29
I need to define a global variable that many files will see it, this variable is a pointer to class. but i failed to do it.
I have a set of classes each in separated files
Jul 19 '07 #1
16 5895
RedSon
5,000 Expert 4TB
What code do you have already? Declaring a class pointer should be as simple as ClassName *pMyClass;
Jul 19 '07 #2
aeo3
29
yeap, but i need all other files see it
Jul 19 '07 #3
RedSon
5,000 Expert 4TB
yeap, but i need all other files see it
If you declare something outside of any brackets it becomes global. If you want to use it in another file you will have to use the extern keyword. Or you can also declare it in a header and have both cpp files include that header.
Jul 19 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Consider not using a global variable but using a Singleton object instead.

Read this.
Jul 19 '07 #5
aeo3
29
I have four files x.h, y.h, z.h and w.cpp
each _.h contains a class. i need for example x.h and z.h deal with a class that exists in z.h
Jul 19 '07 #6
ravenspoint
111 100+
I have four files x.h, y.h, z.h and w.cpp
each _.h contains a class. i need for example x.h and z.h deal with a class that exists in z.h
Suppose you have a class T, whose interface is deined in file t.h

Create a global object of class T, outside of any brackets in your mainline file

Expand|Select|Wrap|Line Numbers
  1. #include "t.h"
  2.  
  3. T global_t;
Allow anyone who wants to see your global by adding to t.h the following line, near the end, outside any brackets

Expand|Select|Wrap|Line Numbers
  1. extern T global_t;
Jul 19 '07 #7
aeo3
29
but i need it as a pointer to that class when i tried to do, functions in other classfiles didnot see it.
Jul 19 '07 #8
ravenspoint
111 100+
but i need it as a pointer to that class when i tried to do, functions in other classfiles didnot see it.
So? Make it a pointer then.

in mainline
Expand|Select|Wrap|Line Numbers
  1. T t;
  2.  
  3. T * global_ptr_T;
inside main()

Expand|Select|Wrap|Line Numbers
  1. global_ptr_T = &t;

in t.h

Expand|Select|Wrap|Line Numbers
  1. extern T* global_ptr_T;
Jul 19 '07 #9
aeo3
29
It works now ,
thank you
Jul 19 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
extern T* global_ptr_T;
Just be aware that the object may not have been created before you use the pointer. This is one of the cases against using global variables: The Initialization Fiasco:

Expand|Select|Wrap|Line Numbers
  1. MyClass  AnotherGlobalObject(global_ptr_T);
  2.  
There really is no case where a global variable is required.

I say again, try using a Singleton object.
Jul 19 '07 #11
ravenspoint
111 100+
Just be aware that the object may not have been created before you use the pointer.

Please look, carefully, at the first code fragment where I create the object before taking its address and assigning it to the global pointer.

No fiasco this time :-)
Jul 19 '07 #12
ravenspoint
111 100+
Expand|Select|Wrap|Line Numbers
  1. MyClass  AnotherGlobalObject(global_ptr_T);
  2.  
To fix this, write
Expand|Select|Wrap|Line Numbers
  1. MyClass  AnotherGlobalObject( &t );
  2.  
  3.  
Jul 19 '07 #13
weaknessforcats
9,208 Expert Mod 8TB
Please look, carefully, at the first code fragment where I create the object before taking its address and assigning it to the global pointer.
That's not the issue. Your pointer is valid.

It's just in this other file:

Expand|Select|Wrap|Line Numbers
  1. extern extern T* global_ptr_T;
  2. MyClass  AnotherGlobalObject(global_ptr_T);
  3.  
  4. int main()
  5. {
  6.  
  7. }
  8.  
that the global pointer may not be valid. There is no guarantee as to the order which .cpp files get initialized.
Jul 19 '07 #14
ravenspoint
111 100+
That's not the issue. Your pointer is valid.

It's just in this other file:

Expand|Select|Wrap|Line Numbers
  1. extern extern T* global_ptr_T;
  2. MyClass  AnotherGlobalObject(global_ptr_T);
  3.  
  4. int main()
  5. {
  6.  
  7. }
  8.  
that the global pointer may not be valid. There is no guarantee as to the order which .cpp files get initialized.

This would not compile - you would have two files containing main().

Perhaps I should have emphasized more strongly that all globals should be declared in the file that contains main(), between the includes and the Main() entry point. In this way we have control over the initialization order.

Yes, if you sprinkle global declaration all over the place, chaos will be the result.

Also, I do agree with you that globals should be avoided wherever reasonable. I like to use the phrase "polluting the global namespace" to motivate myself to take the trouble to avoid them. However, in the the real world of quick and dirty programs to solve today's problem, using a few globals can save an amount of design work that is inappropriate to the task at hand.
Jul 19 '07 #15
weaknessforcats
9,208 Expert Mod 8TB
Perhaps I should have emphasized more strongly that all globals should be declared in the file that contains main(), between the includes and the Main() entry point. In this way we have control over the initialization order.
Yes. But this has the restriction that you can't use these globals in another program without carrying main() along. So you have top make a copy and maintain duplicated code.

The globals do need to be in one place and it is not the file with main() in it. It is another .cpp file. These globals should be in an anonymous namespace accsssed by a Singleton object. That way you can reuse the globals in many programs.

On a broader note, globals are not good for these reasons:
1) local variable hides global variable
2) name conflicts. even within namespaces.
3) exposes implementation. No redesign
4) causes race conditions in multithreaded programs
5) expands ripple when value is screwed up. every function is a suspect
6) no guarantee the user will use the global
7) expands program footprint
8) memory for globals may be limited
9) no guarantee for the order of creation. Only the globals in a single file are crerated in the order of declaration. The total order is indeterminate. Hence, your global may no be there when you need it. Especially if a global object needs a global variable in another file in its constructor. aka: the initialization fiasco
10) expands ripple when a recode is needed. All the code using globals has to be changed. Bad if there is a large installed base.
Jul 19 '07 #16
ravenspoint
111 100+
Yes. But this has the restriction that you can't use these globals in another program without carrying main() along. So you have top make a copy and maintain duplicated code.
I am sorry, I do not understand this point. Why would you want to use the same globals in another program? What would it mean if you did? In any case, I am fairly sure the OP did not want to do any such thing, he just wanted to get his program working ( which he did ) and perhaps get some advice on how to avoid such problems in future


On a broader note, globals are not good for these reasons:
I have already agreed, globals are not good. You are beating a dead horse.
Jul 19 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: none | last post by:
Hello all, I wrote a shell program a few years ago in VB6 that needs to be modified. The problem I have is this: The SysAdmin uses this shell in place of Explorer, so there is no taskbar. When...
10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
6
by: james | last post by:
CAn Some one please help when this scrip runs it does not enter the data from my forms, it just says where each feild should be. I have tested it using google address and it appears to work just...
6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
8
by: yinglcs | last post by:
Hi, I have the following code: colorIndex = 0; def test(): print colorIndex; This won't work. But it works if i do this:
9
by: djconner | last post by:
I'm a total neophyte in Javascript, trying to hack something together fast, and run into something that I can't understand at all. Two loops appear in sequence. The idea is that arrQuestions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.