Is it possible to create a getter for a private array?

I currently have a private bool array, in class A that I would like to use in class B. Class B has access to has a list of class A, and for each object of Type A, I want to loop through the class A's array and do something.

1
2
3
4
5
6
7
//Class A declaration.
bool links [9]{false, false, false, false, false, false, false, false, false};
bool *Track::getLinks()
{
    return links;
}


1
2
3
4
//Class B
bool[9] currentLinks = track->getLinks();
        for (bool link : currentLinks)
Last edited on
You can declare class B as a friend of class A, or you can provide a function that grants access to each array element.
a pointer pretty much bypasses public/private concerns.
with getlinks() the user can do what they will to the memory, there is no protection.

if that bothers you, you can return a copy, or use a vector, or try to force a const on it (this is still bypassable but you have to do it actively rather than passively/by mistake).
a pointer pretty much bypasses public/private concerns.
with getlinks() the user can do what they will to the memory, there is no protection.

True, but in that sense there is no protection to any memory in the process. If you have access to the declaration of a class, you can gain easy access to all the members by creating a shadow class where everything is public.

Access specifiers are just a gentleman's agreement.
Other options include
a. wrap the array within a std::array;
b. overload operator[] for the class;
c. ...
Last edited on
Agreed Dhayden, but here, its more than that, hes is returning the pointer directly, so the user can say
x = something.getlinks();
...
x[3] = true; //unusual access rights, and worse, user may not know its size and go out of range.

this is way different from intentionally accessing something with syntax exploits.
Last edited on
Excellent point, jonnin. Thanks.
with getlinks() the user can do what they will to the memory, there is no protection.
True enough for the first bit but C-style arrays 101 says they must always carry their size with them or words to that effect. So there is protection despite the ease of forgetting it.

There is also no doubt that <vector>s or similar built-in protection are the reliable solution.
Returning to the original question:
bool[9] currentLinks = track->getLinks();
That's illegal. You can't assign an array from a pointer. How about returning a pointer to const and then copying the values into class B:
1
2
3
4
5
6
7
8
9
10
11
//Class A declaration.
bool links [9]{false, false, false, false, false, false, false, false, false};
const bool *Track::getLinks()
{
    return links;
}

//Class B
bool[9] currentLinks;
for (int i=0; i<9; ++i) currentLinks = track->getLinks()[i];  // copy track's links
for (bool link : currentLinks)


You could also have Class A return individual tracks:
bool Track::getLink(unsigned index) { return links[index]; }
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
#include <iostream>

class A
{
private:
    bool links[9]{false, true, false, false, false, true, false, false, false};
    
public:
    A(){}
    bool* getLinks(){return links;}
};

class B
{
public:
    B(){}
    bool* getLinks(A some_A) { return some_A.getLinks(); }
};

int main()
{
    A a;
    B b;
    
    size_t size{9}; // 9 IS GLOBAL CONSTANT, OR MAKE A::getSIZE() METHOD
    
    for(size_t i = 0; i < size; i++)
    std::cout << b.getLinks(a)[i] << '\n';
    
    return 0;
}
Topic archived. No new replies allowed.