Accessing Variables Between Classes

Lets say I have my main class declared in a header file, and defined in a cpp file.

Main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef MAIN_H_
#define MAIN_H_

#include <iostream>

class Application
{
public:

     Application()
     {
     }

     ~Application()
     {
     }

     int i = 3
     i* integer;

protected:

     void function(void);
};

#endif 


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iotream>
#include "Main.h"
#include "Header.h" // Getting to this.


void Application::function()
{
     cout << "Hello world." << endl;
}

Application::function();

return 0;


Now let's say I have a second class in a header file.

Header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef HEADER_H_
#define HEADER_H_

#include <iostream>

class anotherClass
{
public:

     int i = Application.integer; // I know this won't work.
}

#endif
{


What I'm trying to do is access functions, variables, and pointers of one class from an entirely different class. I know that I can call functions by making them static and using "class_name::function();" but I haven't been able to figure out variables and pointers.

There may be mistakes in the code above, as I just wrote it. This isn't the exact code I'm working with, as it would be too long and complex to reasonably ask a question about here. However, it gets the idea across.

Thanks for any help, I appreciate it.
Last edited on
Are you talking about something like in one file you have an instance of a class called Application and in another file you want to be able to get stuff from that instance? Or do you just want a class to be able to get stuff that type of class in general?
I just need to pass variables between classes. The only reason I posted my Application class as being separated into a header and a main was because that's the structure my actual application follows, even though it will probably not affect the solution.

In my actual application, I have a pointer to an object (declared in my main class) that I want to be able to access from several different classes.
Last edited on
let me see if I understand you correctly:
For example:
You have two classes A and B.
You want to pass an object of class A to a member function of an object of class B and have class B have access to ALL of class A's private as well as public members/functions?

If so - Then you are talking about friend classes.
See here: http://www.learncpp.com/cpp-tutorial/813-friend-functions-and-classes/

There is loads of information around - just google.

Note: that friends defeats the point of data encapsulation.
Yes, I would suggest just making a function to access the variable and using that wherever it is needed.
I would recommend to define setter and getter for each data member so that it can be accessed/set from other classes too.

And if you are looking for a "friend" kind of access, note that, a friend relationship is "not inheritable".

Check it out. Good luck :)
Topic archived. No new replies allowed.