C++ Variable Transfer Help - Ask for Advice

Click For Summary

Discussion Overview

The discussion revolves around the transfer of variables between functions in C++, focusing on methods such as global variables, pass-by-reference, and object-oriented programming principles. Participants explore various approaches and their implications for code structure and maintainability.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants suggest declaring global variables for accessibility, though they acknowledge this practice can lead to poor code quality.
  • Others argue that passing arguments by reference is a better practice, as it avoids the pitfalls associated with global variables.
  • One participant emphasizes the importance of understanding pass-by-value and pass-by-reference, suggesting that frequent data transfer between functions may indicate a need for better program structure, potentially using classes or structs.
  • Another participant highlights the appeal of functional programming, which avoids side effects from global variables, making data flow easier to track.
  • Some participants discuss the concept of ownership in object-oriented programming, where a class controls access to its data through methods, although this can be taken to extremes.
  • A participant introduces the singleton pattern as a modern approach to managing global variables, providing code examples to illustrate the concept.

Areas of Agreement / Disagreement

Participants express a mix of opinions on the use of global variables versus other methods of variable transfer. While there is some agreement on the drawbacks of global variables, no consensus is reached on the best approach, as various models and practices are presented.

Contextual Notes

Some discussions touch on the limitations of global variable usage and the need for careful program structure, but specific assumptions and unresolved issues remain unaddressed.

RandomAllTime
Messages
26
Reaction score
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
You can always declare global variables, although this is quite ugly. Better is to pass an argument by reference to a function you call.
 
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   Reactions: elusiveshame and RandomAllTime
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   Reactions: RandomAllTime
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   Reactions: RandomAllTime
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.
 
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.
 
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.
 
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;
}
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
Replies
4
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K