C++ Variable Transfer Help - Ask for Advice

In summary, globals can be accessed by all functions in the same file, whereas variables declared outside of "{}" brackets are global to the file in which they are defined, but are also accessible to linked programs. A better way to do global variables is to use a singleton. Singletons allow you to create an object which has only one instance.
  • #1
RandomAllTime
26
0
I am just getting into C++ and I would like to ask you guys for some help regarding transferring variables form one function to another. Is there any way I can declare a variable such that it can be referenced and changed throughout all the programs functions? Thanks.
 
Technology news on Phys.org
  • #2
You can always declare global variables, although this is quite ugly. Better is to pass an argument by reference to a function you call.
 
  • #3
Believe me, if you try to write serious code (i.e. a program of non-trivial size that includes a non-trivial number of functions), that uses global variables, when it comes time for someone else to fix a bug or add a feature to it, they will want to string you up by your thumbs, douse you with some sugary soft drink, and set a bucket full of fire ants loose on you. I speak from experience here. :mad:

Learn about pass-by-value and pass-by-reference. If you find yourself passing a lot of related data back and forth between functions, that's a sign that you should think carefully about your overall program structure, and consider using classes (objects) or structs to bundle things together.
 
  • Like
Likes elusiveshame and RandomAllTime
  • #4
This is also the reason that functional programming is so appealing. Functions have no side effects ie changing global variables. Its harder to write the code but youll be able to track how your data flows through your code.
 
  • Like
Likes RandomAllTime
  • #5
An important concept of C++ and object oriented programming is that there is one class that "owns" a piece of information. If other parts of the program want to change that data, they must call a method of the owning class. That way, the owning class can control changes. Unfortunately, I have seen this carried to ridiculous lengths.

There are simple ways to make data globally accessible. A variable declared outside of any "{}" brackets is accessible to any function defined in that file and is also accessible to linked programs unless it is declared 'static'. You can make entire structures of data available to all programs. This places the responsibility on the programmers to use the access wisely. As others have said, you might be disappointed.
 
Last edited:
  • Like
Likes RandomAllTime
  • #6
jtbell said:
Believe me, if you try to write serious code (i.e. a program of non-trivial size that includes a non-trivial number of functions), that uses global variables, when it comes time for someone else to fix a bug or add a feature to it, they will want to string you up by your thumbs, douse you with some sugary soft drink, and set a bucket full of fire ants loose on you. I speak from experience here. :mad:

Learn about pass-by-value and pass-by-reference. If you find yourself passing a lot of related data back and forth between functions, that's a sign that you should think carefully about your overall program structure, and consider using classes (objects) or structs to bundle things together.
Thanks I will learn that.
 
  • #7
FactChecker said:
An important concept of C++ and object oriented programming is that there is one class that "owns" a piece of information. If other parts of the program want to change that data, they must call a method of the owning class. That way, the owning class can control changes. Unfortunately, I have seen this carried to ridiculous lengths.

There are simple ways to make data globally accessible. A variable declared outside of any "{}" brackets is accessible to any function defined in that file and is also accessible to linked programs unless it is declared 'static'. You can make entire structures of data available to all programs. This places the responsibility on the programmers to use the access wisely. As others have said, you might be disappointed.
Thank you, I appreciate it.
 
  • #8
jedishrfu said:
This is also the reason that functional programming is so appealing. Functions have no side effects ie changing global variables. Its harder to write the code but youll be able to track how your data flows through your code.
Thanks, I appreciate the input.
 
  • #9
The modern way to do a global variable is to use a singleton.

Globals:

main.c
Code:
#include "other.h"
int myIntVariable = 1;
int main(){
     int result = other_func(5);
     return 0;
}

other.h
Code:
#ifndef __OTHER_H
#define __OTHER_H

int other_func(int var);

#endif

other.c
Code:
#include "other.h"
extern int myIntVariable;
int other_func(int var){
   int result = var * myIntVariable;
   myIntVariable = var;
   return result;
}
Singletons:

main.cpp
Code:
#include "other.h"

int main(){
     int result = other::func(5);
     return 0;
}

other.h
Code:
#ifndef __OTHER_H
#define __OTHER_H

class other {
public:
     static int & myIntVariable();
     static int func(int var);
};
#endif

other.cpp
Code:
#include "other.h"
int & other::myIntVariable(){
     static int myVar = 1;
     return myVar;
}

int other::func(int var){
   int result = var * myIntVariable();
   myIntVariable() = var;
   return result;
}
 

FAQ: C++ Variable Transfer Help - Ask for Advice

1. How do I transfer variables between functions in C++?

In C++, variables can be transferred between functions by passing them as parameters in the function declaration. This allows the function to access and manipulate the variable's value. Alternatively, you can use global variables, but this is generally not recommended as it can lead to unmanageable code.

2. What is the difference between pass by value and pass by reference in C++?

In pass by value, a copy of the variable's value is passed to the function, so any changes made to the variable within the function will not affect the original value. In pass by reference, a reference to the variable is passed, allowing the function to directly access and modify the original value.

3. How can I pass multiple variables to a single function in C++?

In C++, multiple variables can be passed to a function by separating them with commas in the function declaration, and then passing the corresponding values as arguments when calling the function.

4. What is the scope of a variable in C++?

The scope of a variable in C++ refers to the part of the program where it can be accessed and used. Variables declared within a function have local scope and can only be accessed within that function. Variables declared outside of any function have global scope and can be accessed by any function in the program.

5. Can I transfer variables between C++ classes?

Yes, variables can be transferred between C++ classes by using class functions to access and manipulate the variable's value. Alternatively, you can also use friend classes to allow one class to access private variables of another class.

Similar threads

Replies
25
Views
1K
Replies
2
Views
1K
Replies
3
Views
966
Replies
36
Views
2K
Replies
2
Views
1K
Replies
34
Views
3K
Replies
23
Views
2K
Replies
13
Views
2K
Back
Top