Access to protected variable in library


it's possible to 'get' value from protected variable in library ?


The library "Adafruit_SSD1325" have:


setCursor(int16_t x, int16_t y), setTextColor(uint16_t c), setTextColor(uint16_t c, uint16_t bg), setTextSize(uint8_t s), setTextWrap(boolean w), setRotation(uint8_t r),

protected: uint8_t textsize; uint8_t rotation;

It's possible to get value of "textsize"?

I need to use ?

Sure, delete 'protected:'.

Python doesn't even use protected or private, because they offer no real protection or privacy at all . . . if you want to be naughty you can.

But that's kind of evil since it's probably protected for a good reason. I'd suggest rethinking your problem. Either you're using the library incorrectly or the library is badly designed.

Yes there is. Three ways infact without touching the class.

  1. Create a class which derives off the class.

if the class is called: Adafruit_SSD1325 then you can simply do this:

class MySSD : public Adafruit_SSD1325{
    uint8_t getTextSize() { return textsize; }

Why? because derived classes have access to protected members of base classes.

You can simply use this class instead of the Adafruit_SSD1325 class. All public functions available in the base are available in the derived class.

If the SSD class uses constructors for passing data, you'll need to add in constructors which can pass the variables through to the base.

  1. A rule breaking pointer cast to the offset of the variable (not recommended, but should work).

  2. A template system which will allow you to read the value directly (more code than its worth, go with option 1).

pYro_65: If the SSD class uses constructors for passing data, you'll need to add in constructors which can pass the variables through to the base.

Nice ;)

I've tried to do that but i can not create the builder :/

class MySSD1325 : public Adafruit_SSD1325{ public: MySSD1325 display : Adafruit_SSD1325(pin_oled_mosi,pin_oled_clk,pin_oled_dc,pin_oled_reset,pin_oled_cs); uint8_t getTextSize() { return textsize; } };

You have a few errors there.

See if you can follow this example, it shows what is needed.

class Base{

    //Base class constructor
    Base( int _a,  char _b ) : a(_a), b(_b) {

    int a;
    char b;
    char textsize;

class Derived : public Base{


    //Constructor, which also constructs Base
    Derived( int _a,  char _b ) : Base( _a, _b ) {   

    char getTextSize(){
      return textsize;

//The actual object you will use, named derivedObj.

Derived derivedObj( 1, 2 );

void setup() {

void loop() {}


My code:

class MySSD1325 : public Adafruit_SSD1325{ public:

MySSD1325(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS) : Adafruit_SSD1325(SID, SCLK, DC, RST, CS) {}

uint8_t getDisplaySize() { return textsize; }


MySSD1325 display = MySSD1325(pin_oled_mosi,pin_oled_clk,pin_oled_dc,pin_oled_reset,pin_oled_cs);

now, i have my function "getDisplaySize()" but main methods not exists :/

If done correctly it should work. For example if I add void foo(){} to the base class I can call it using the derived class:;

I'll see if I can find the library, but post yours too. Use the code box and post what you actually have.


[code]Any code here will be inside its own box.[/code]

Sorry... code works well :)


Best Regards, JP

No worries, watch out if you download the latest version of the Adafruit_GFX lib, there is a bug in it that was supposed to be fixed. I'm gonna let them know now.