Accessing 1 private member from class

I need to access 1 private member from my class, I have been looking up how to do it but nothing that, really seemed to help me. I seen a post that said to use friend but i dont know.

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
50
#ifndef SCROLLBARCLASS_H_INCLUDED
#define SCROLLBARCLASS_H_INCLUDED

class Scrollbar
{
    public:
        Scrollbar();
        ~Scrollbar();
        void CreateScrollBar(int scrollbarWidth, int scrollbarHeight);
        void SetScrollbarPosition(int &scrollbarPos_x, int &scrollbarPos_y);
        void SetScrollbarColor(sf::Color(int red, int green, int blue));
        void S

    private:
        sf::RectangleShape scrollBar;
        int scrollbarHeight;
        int scrollbarWidth;
        int scrollbarPos_x;
        int scrollbarPos_y;
        int red;
        int green;
        int blue;
};

Scrollbar::Scrollbar()
{

}

Scrollbar::~Scrollbar()
{

}

void Scrollbar::CreateScrollBar(int scrollbarWidth, int scrollbarHeight)
{
    scrollBar.setSize(sf::Vector2f(scrollbarWidth, scrollbarHeight));
}

void Scrollbar::SetScrollbarPosition(int &scrollbarPos_x, int &scrollbarPos_y)
{
    scrollBar.setPosition(scrollbarPos_x, scrollbarPos_y);
}

void Scrollbar::SetScrollbarColor(sf::Color(int red, int green, int blue))
{

}

#endif // SCROLLBARCLASS_H_INCLUDED 
Private members can't be accessed from other classes. That's the whole point. If a class needs to access a private member of another class, that's very often a sign that you need to reevaluate your design.
Oh no I'm sorry, I forgot to say that I need to access it in my main function.

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
#include <SFML/Graphics.hpp>
#include <iostream>

#include "ScrollbarClass.h"
#include "ButtonClass.h"
#include "Prototypes.h"

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;

    Scrollbar SB;
    SB.CreateScrollBar(5, 7);

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                }break;
            }
        }
    	window.clear(sf::Color::White);

        window.draw(SB.scrollBar);
    	window.display();
    }

    return 0;
}
Last edited on
Scrollbar could inherit sf::RectangleShape, then any place you want to pass RectangleShape, you can pass a Scrollbar instance.

1
2
3
4
5
  class Scrollbar : public sf::RectangleShape
...
   Scrollbar SB; 
...
  window.draw(SB);







That Scrollbar has a RectangleShape is IMO somewhat incidental, rather than an inherent property of a scrollbar.
The correct solution would be to implement Scrollbar::draw(sf::RenderWindow &).
if you only need to read a private member, add a member function that returns it. You can return a const reference to save memory and save the cycles taken from copying (aka: more efficient).

Example:

1
2
3
4
5
6
7
8
9
public:
    const int& get_int() const
    {
         return this->my_int;
    }

private:

    int my_int;
Topic archived. No new replies allowed.