help - how to get values from different classes

Hello everyone,

I am learning c++ and enjoying it a lot.
Probably this is a very basic question, here goes anyway.

Lets assume the following:

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
27
28
29
30
class A{
   int value_a = 100;

   void setValue(int someValueforA){
      value_a = someValueforA;
   }

   int getValue(){
      return value_a;
   }
};


int main(){
   int mailValue_0 = 0;
   int mainValue_1 = 42;
   A valueA;

   valueA.setValue(mainValue_1);
   mainValue_0 = valueA.getValue();
   return 0;
}

class B{

   int b;
   A valueFromA;
   
   b = valueFromA.getValue();
};

... So my problem is, i want to get the value of "value_A", (from classA) on class B. Basically i have main() and classB interacting with classA values. But when i do the above i believe it's wrong because when i do "A valueFromA;" on classB i'm getting a completely new object, right??

What is the right way on doing this?
Hope its not too confusing.
Last edited on
Please use code tags in the future.
http://www.cplusplus.com/articles/z13hAqkS/

You are correct. This is a very contrived example so it is hard to recommend a good kind of strategy to use, but you can pass the A instance to a method of B (perhaps the constructor) and use it to initialize those values.

Other notes:
- main() must return an int
- You are missing semicolons after your class definitions
Sorry for tag missing and thanks for the tips.

Could you write a simple example of what you mean please?
My goal is to have more classes other than B using class A's values.

Thanks for the notes.
You can edit your post to add code tags; that would help reading it.

Do you have a more concrete example of what you are trying to accomplish with these classes? I could write something for what you have here but I don't think it would necessarily be generally applicable to a particular situation.
visitor design pattern

or friendship
If you're familiar with keyword friend, it can help out when you want a certain function to be applicable for another class. Either that or you can just declare an entire class into another class, under your public or private sections.

http://stackoverflow.com/questions/6407691/friend-declaration-in-c-difference-between-public-and-private

http://www.cplusplus.com/doc/tutorial/inheritance/
Last edited on
But when i do the above

You do realise that in your code you never create an object of type B?
@YFGHNG many thanks for showing me that keyword. I just learned something new.
@mutexe, yes i know i didn't create an object o type B for example purposes.
I gave that simple example to explain what i needed. Perhaps it was too simple. I will try to give a more elaborated one:

I want to have a class that gathers and stores information about all hard drives present in the system, (classA). On classB i want to be able to access the classA's struct values and change it if needed and on main(), i want to "read" classA's struct values wether or not those values were changed on classB.

My real problem is how i can share values from a class to others classes and not with the code it self. I apologize for not using real code in the functions because i am using qt framework and don't really know how to not use it to get an hard drive list and it is not my goal to know it, for now.

With all this in mind i'm using a struct array like.

1
2
3
4
struct hardDiskStruct{
   string driveName, driveFileSystemType, state;
   int spaceFree;
};




This struct will be used on class A.

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
27
28
29
30
31
32
33
34
35
36
37
38
// for the sake of this example i hardcoded 3hdd
hardDiskStruct hardDiskList = new hardDiskStruct[3];

hardDiskStruct classA::get_hddList(){

   int counter = 0;

   // ... some c++ functions to retrieve current system's hdd
   // please assume "localdrive" can read the needed hdd stuff.

   foreach (localdrive){
      hardDiskList[counter].driveName = locadrive(drive name)
      hardDiskList[counter] driveFileSystemType = localdrive(drive filesystem type)
      hardDiskList[counter].spaceFree = localdrive( free space available)
      hardDiskList[counter].state = "free";
      counter++;
   }

   return hardDiskList; 
}


int main{
   
   // i want to have access to classA hardDiskList array values 
   // to know which disks are available.
  
   return 0;
}

classB{

   // i want to have access to classA hardDiskList array values to know which disks
   // are available and change the state to "busy" when needed.



}



After all this i read in the tutorials section about "static"! Could i use "static" keyword?
Please give me simple examples.
If you want one piece of code, whether it's in a class or not, to have access to data elsewhere, pass a reference (or pointer) to that data to the code you want to have access.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// http://ideone.com/P2KvDm
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

struct A
{
    A(std::initializer_list<int> il) : _values(il) {}

    std::vector<int>& get_values() { return _values; }

private:
    std::vector<int> _values;
};

struct B
{
    B(A* a) : _a(a) {}

    void apply_filter(std::function<bool(int)> filter)
    {
        std::vector<int>& v = _a->get_values();

        v.erase(std::remove_if(v.begin(), v.end(), filter), v.end());
    }

private:
    A* _a;
};

void print(const std::vector<int>& v)
{
    std::cout << "{ ";
    for (auto e : v)
        std::cout << e << ' ';
    std::cout << "}\n";
}

int main()
{
    A a({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    print(a.get_values());

    B b(&a);
    b.apply_filter([](int val) { return val % 2 == 0; });

    print(a.get_values());
}
Topic archived. No new replies allowed.