473,407 Members | 2,326 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,407 software developers and data experts.

Passing global variables

23
I am trying to do an assignment where I declare at first 4 variables (actually they are linked lists), and try to pass them through 10 functions where only the last one actually uses them.
Is there a way so that all my functions don't have to hold 4 parameters which they do not use, but they only pass? a kind of global variable accessed from anywhere (even if my functions are located in a header file)?

I tried to declare them as global variables outside the main, and I tried to declare them on a separate namespace, but it still gives me errors

Thank you for your time.
Jan 7 '08 #1
5 1855
Savage
1,764 Expert 1GB
I am trying to do an assignment where I declare at first 4 variables (actually they are linked lists), and try to pass them through 10 functions where only the last one actually uses them.
Is there a way so that all my functions don't have to hold 4 parameters which they do not use, but they only pass? a kind of global variable accessed from anywhere (even if my functions are located in a header file)?

I tried to declare them as global variables outside the main, and I tried to declare them on a separate namespace, but it still gives me errors

Thank you for your time.
If they are in same file as main,are they above includes that uses them?
If you can't manage this, declare the globals into a header file,where your functions are and then include it into the main file..

Savage
Jan 7 '08 #2
RRick
463 Expert 256MB
This is where objects in C++ come in handy. Take the 4 lists and make them part of an object, either as static or local variables.

Now all of the static/local methods in that object will have access to those lists without having to explicitly pass them.

For example, the static Convert method would be alble to access List1_ and List2 without any parameter passing.
Expand|Select|Wrap|Line Numbers
  1. class ProcessSomething
  2. {
  3.     static List<Int> List1_;
  4.     static List<Int> List2_;  //  etc.
  5.  
  6. public:
  7.     static Convert( void);
  8. };
Jan 8 '08 #3
rpm27
23
Great idea, I'll create a class (I'll call it Main) with private members the 4 lists, and all the functions are public. But so I don't have to call each function Main.function(), should've the functions be friend functions?
I'll try it right away. Thank you
Jan 8 '08 #4
RRick
463 Expert 256MB
I suppose there are ways/tricks to get rid of the Main:: prefix, but I actually prefer it. I use it like you would a namespace. BTW: For static routines you will use a Main::function declaration.

To avoid name duplication, you can make the class name more descriptive and then simplify the function name. For example, instead of Main::accountBalanceReport, you could have an class Account and then the function would be balanceReport. To me, Account::balanceReport makes a lot of sense. You are not stuck with one class for all of the functions
Jan 8 '08 #5
rpm27
23
I retried to declare them with global variables and it worked.
I has to add the #if !defined statements.
Thank you.
Jan 9 '08 #6

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

Similar topics

0
by: lawrence | last post by:
Those of you with backgrounds with the C language will laugh at my mistake, but those of you, like myself, who deal mostly with PHP should be warned about passing variables as references -...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among...
1
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could...
3
by: jeffg | last post by:
The answer probably is staring me in the face and I don't see it, so I'll just ask. I try to limit the scope of variables and avoid global variables. But sometimes I get stumped by the following...
4
by: hello smith | last post by:
I have a lot of functions that add values to an array. They alos update a global variable of type int. Currently, I use a global variable to hold this array. All functions access this array...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
7
by: andy | last post by:
A question about about passing a class by reference: Say you have a class called car, and within that you have two objects called car01 and car02. Within the class I have an int variable...
1
satterfieldben
by: satterfieldben | last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would call...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.