"Get" function for a vector

I am working on an OPP code and I want to write a get function for a vector.
I have a vector and each element of the vector is a class and there is a vector in that class and I want to get access to that class:

1
2
3
4
5
6
7
8
9
10
11
12
13
class TemperatureRepo{
private:

vector<Cell*> cls; // pointer vector for the whole study area
};

class Cell{
private:
std::vector<double> AirT;

public:
std::vector<double> getAirT() { return AirT; }
};


considering mentioned situation, I want to get access the AirT vector in the following class. I was wondering if it is possible? Otherwise, what is the best alternative?

1
2
3
4
5
6
7
8
9
class Temperature {

TemperatureRepo* Repo;
Cell* cls;

std::vector<double> tempAitT = Repo->cls[15]->getAirT();

};


Thanks.
Assuming that AirT is non-static (as shown in your code) you can use public setters and getters:
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
#include <iostream>
#include <vector>

class Cell
{
    private:
    std::vector<double> AirT;

    public:
    set_AirT (const std::vector<double> original) { AirT = original;}
    std::vector<double> getAirT()const { return AirT; }

};
class Temperature
{
    private:
    Cell* cls;

    public:
    Cell* get_cls()const {return cls;}
    set_cls (Cell* myCell){cls = myCell;}
};
int main()
{
    Cell c;
    std::vector<double> original {0, 1, 2, 3, 4};
    c.set_AirT(original);

    Temperature temp;
    temp.set_cls(&c);
    std::vector<double> vec = temp.get_cls()->getAirT();
    for (auto& elem : vec)
    {
        std::cout << elem << " ";
    }
}

Output
 
0 1 2 3 4 




closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/205734/#msg974738
Topic archived. No new replies allowed.