C++ Passing Variables Between Classes

In summary, the programmer is trying to pass a variable from one class to another class, but is getting garbage values when they try to display the variable.
  • #1
chronie
23
0
Hi,

I am new to programming, but I think this is a simple error. For my programming code I am trying to pass a variable from a certain class to another class. The code is similar to below.Class 1
PHP:
#include "class2"

Class2 class

var1=1
var2=2

class.acceptingvariablesfrom1(var1 var2)

Class 2
PHP:
class.acceptingvariablesfrom1(var1 var2)

cout << var1 << endl;

That is what my basic code looks like. I send my var1 and var2 to my acceptingvariablesclass however, how do I have them displayed. When I do my cout statement I get garbage. Am I doing something wrong? Class1 and Class2 are different .cpp files. In Class2 do I have to define var1 and var2 again even though they were defined in class1?

I think this is a basic question but I just don't understand why I am getting garbage values?
 
Last edited:
Technology news on Phys.org
  • #2
You need to use commas to separate the variables.
 
  • #3
I'm sorry, in my real code I had commas. I am just confused as to why garbage values print out.
 
  • #4
Post the actual code with declarations. This sample is nothing. (what are var1 and var2? ) And what param types is the function expecting?

Garbage could be from anywhere in your code if you trashed a pointer somewhere.

Create a small simple program that tests the classes, that will at least isolate whether it is the class or not.
 
  • #5
Agreed. The best thing to do is construct a short, complete example program that can be pasted into a compiler and compiled successfully, and that demonstrates the problem.
 
  • #6
Besides, with this approach 9 times out of 10 you will find the problem by yourself.
 
  • #7
Borek said:
Besides, with this approach 9 times out of 10 you will find the problem by yourself.

Yep. My daughter is a Comp Eng and she took a Comp Sci programming course, which was C at her University. I was shocked at how the prof. didn't teach the students proper style or technique. She stopped showing me her projects because I would get on her.

Not initializing pointers? That is not a style preference, it is a must do.

I think that is where the experience really comes into play; debugging.
 

Related to C++ Passing Variables Between Classes

1. How do I pass a variable between two classes in C++?

To pass a variable between two classes in C++, you can use a variety of techniques such as passing by reference, passing by pointer, or using a public member function. Passing by reference allows the called function to directly access and modify the original variable, while passing by pointer allows the called function to indirectly access and modify the original variable. Using a public member function allows for controlled access to the variable through the class's public interface.

2. Can I pass a variable between two classes without using pointers or references?

Yes, you can also pass a variable between two classes in C++ by using a return statement. In this case, the variable is copied and returned by the called function, and the original variable remains unchanged. This method is useful for passing immutable data types or when the variable needs to be returned to the calling function.

3. What is the difference between passing by reference and passing by pointer?

Passing by reference and passing by pointer both allow for indirect access to variable in C++, but there are some key differences. With passing by reference, the called function can directly access and modify the original variable, while with passing by pointer, the called function must dereference the pointer to access and modify the variable. Additionally, passing by reference cannot be null, while passing by pointer can be null, leading to potential runtime errors.

4. How can I ensure that the variable passed between classes is not accidentally modified?

To prevent accidental modification of a variable passed between classes in C++, you can use the const keyword when declaring the variable. This will make the variable immutable and any attempts to modify it will result in a compile-time error. Additionally, using a getter function to access the variable rather than directly accessing it can also prevent accidental modification.

5. Are there any other ways to pass variables between classes in C++?

Yes, there are other ways to pass variables between classes in C++. For example, you can use friend functions or classes to allow access to private member variables. You can also use global variables, although this is generally not recommended as it can lead to potential conflicts and make the code less modular. Ultimately, the best approach will depend on the specific needs and design of your program.

Similar threads

Replies
63
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top