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.
#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.
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:
constint& get_int() const
{
returnthis->my_int;
}
private:
int my_int;