Address of static member of a class

I'm working on an esp32 project with this class fabgl (full lib here). it handles a vga display. Within it, fonts are stored in a Fontinfo data structure. Since these are large, they are stored in a static instance of the class and referenced as such:

fabgl::Font_6x8

I need to feed it through other methods, so I need a reference to a Fontinfo date type. The only way I've found to do this is to create a 2nd instance of the font:

fabgl::FontInfo my6x8  = fabgl::Font_6x8;

void myFunc(Fontinfo *font) {}

{//function call:
...
    myFunc(&my6x8);
...
}

now this does work, but it creates duplicates of these large structures and I have 8 or so fonts I'd like to use, so I'd like to find a better way. How do I get the address of the static instance

this is what I would like to do, but I cant get the syntax right:

myFunc( &fabgl::Font_6x8);

similarly, I cannot create & initiate a pointer to the structure:

fabgl::fontinfo *fontPtr = &fabgl::Font_6x8;

I've tried all the variations I can think of, its either beyond me, or not possible?

If your function does not modify the font (and it probably shouldn't), then you should accept the argument by pointer to const, or preferably, reference to const:

void myFunc(const fabgl::FontInfo &font) {}
myFunc(fabgl::Font_6x8);

That's WAY too much code to expect anyone to dig through. Post a small, complete example that demonstrates the problem.

I didn't expect anyone to, I merely provided it for reference. when I dont provide everything, I get yelled at. :neutral_face:

I did exactly that. did you not make it past the link? (there's more at the bottom of this one too :wink: )

thank you for your suggestion & you are correct, my function does not need to & shouldn't modify the font. I should clarify, my function is actually a sort of init() function for class to which I'm trying to store a reference to the font(i had simplified). honestly, Im wiped, I didn't expect a response before I went to bed, but I just had to try. with your method, I couldn't figure out what variable type I could store that reference in. Only way I got that to work was to store it as a FontInfo type, which just makes another copy of the thing, no?

I've been passing pointers to these structures just fine for a while now & I'd like to stick to that method. I just can't seem to make them from the original static instance (fabgl::myFONT). I have to make a copy, of type FontInfo and then I can pull the address off it & store it in a FontInfo pointer:

fabgl::FontInfo myFont = fabgl::STATIC_FONT;
fabgl::FontInfo *myFontPtr;

myFontPtr = &myFont;   //this works
myFontPtr =  &fabgl::STATIC_FONT;    //pulling the address off the original does not, why?

I'd just like to store the address of the original static instance, is that possible & how?

No, you did not. You posted a snippet. Did you miss the word "complete" in my request? A small, complete example is one that either (1) Compiles and exhibits the same misbehavior as your original code, or (2) Fails to compile and shows the same compiler error as your original code.

The snippet that you posted does neither of those.

@gfvalvo

looked I laid out my question super clear in my prev response (if not my original).

its simple, do you know how to directly pull the address a static class member & store it or not, or not?

here, this outta compile:

#include <Arduino.h>
#include <fabgl.h>
#include <fabui.h>
#define FABGL_FONT_INCLUDE_DEFINITION
#include <userFonts/datactr8.h>

fabgl::FontInfo myFont = fabgl::STATIC_FONT;
fabgl::FontInfo *myFontPtr;

void setup()
{
     myFontPtr = &myFont;                    //this works, but only off a copy

  //   myFontPtr =  &fabgl::STATIC_FONT;   //this does not compile

 // const myFontPtr = &fabgl::STATIC_FONT; //this compiles, but moves the problem

   //at some point I get " a const FontInfo* cannot be assigned to a FontInfo* "
}

void loop()
{
}

if your next reply doesn't contain something helpful, kindly refrain from further cluttering my thread.

I think all you need is to add the const and you can store a ref to it:

#include <Arduino.h>

namespace fabgl {
  struct FontInfo {
    int someVars = 0;
  };
  
  const FontInfo FONT_6x8;
};

const fabgl::FontInfo& my6x8 = fabgl::FONT_6x8;

void setup() {
  Serial.begin(115200);

  Serial.println((String)"Theirs: " + fabgl::FONT_6x8.someVars);
  Serial.println((String)"Ours: " + my6x8.someVars);
  
  myFunc(my6x8);
}

void loop() {}

void myFunc(const fabgl::FontInfo& font) {
  Serial.println((String)"Inside func: " + font.someVars);
}

I had to 'fake' their namespace and class/struct to replicate as I'm not downloading and installing all that but FONT_6x8 is described in their lib as:

extern const FontInfo FONT_6x8 = {
  .pointSize = 6,
  .width     = 6,
  .height    = 8,
  .ascent    = 7,
  .inleading = 0,
  .exleading = 0,
  .flags     = 0,
  .weight    = 400,
  .charset   = 255,
  .data      = FONT_6x8_DATA,
  .chptr     = NULL,
  .codepage  = 437,
};

Although I do wonder... Why do you need to pass references or pointers to the FONT_6x8 at all? They look like they exist essentially as globals so you can just use fabgl::FONT_6x8 inside any function, no?

Nope. Maybe you have a different version of the FabGL library installed. Mine (v1.08) doesn't have a file called 'userFonts/datactr8.h':

sketch_jun28a:5:10: fatal error: userFonts/datactr8.h: No such file or directory
 #include <userFonts/datactr8.h>
          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
userFonts/datactr8.h: No such file or directory

If I comment out that #include, I get:

sketch_jun28a:7:33: error: 'STATIC_FONT' is not a member of 'fabgl'
 fabgl::FontInfo myFont = fabgl::STATIC_FONT;
                                 ^~~~~~~~~~~
exit status 1
'STATIC_FONT' is not a member of 'fabgl'

So, must be something different about your installation or library version. Unfortunately, I can't propose a solution since I'd be unable to test it.

Good luck.

You either store it in another reference, or you take the address of the reference, and store that as a pointer.

void myFunc(const fabgl::FontInfo &font) {
   const fabgl::FontInfo &another_reference = font;
   const fabgl::FontInfo *a_pointer = &font;
}

When you accept an argument by pointer, you signal to the users of your code that nullptr is a valid input.
If that's not the case, you should accept it by reference, not by pointer.

Like I mentioned in my previous post: store it as a pointer to const. You cannot use a pointer to non-const to point to a const variable. (Otherwise you could use that pointer to write to a read-only variable.)

const fabgl::FontInfo *pointer_to_a_font = &fabgl::Font_6x8;

I didn't ignore your comment about const...I tried to implement it. The problem was that, ultimately, needed to be stored inside a variable inside a class instance. and until about an hour ago, I was under the impression that a const had to be initialized @ declaration, so my storage type(in the class) had to be non const (so I thought). I finally did try it as a const just to see what error it would give me & sure enough it was fine.

Little bit of retooling to account the the variable coming in as reference & the storage type now being const . but now it looks like I am back up and running, passing the array by reference, but saving a perm. pointer to it. everything is const & there are no extra copies needed.

Thank you for your assistance...I spent hours trying to figure that out, I doubt I would have without your guidance.

you were also correct about how to fix it..what I didn't know how to do was store that in a const class member (didn't know I could initialize separate from declaration).

yes & no. some of the built in functions take a pointer to a font, but I cant feed it in directly as fabgl::FONT_6x8, it would need to be &fabgl::FONT_6x8 --which is an invalid argument. Where as when I make a local instance of the font, I can directly feed its address in when it wants a pointer.

thats a part of my reasoning, but the more important reason is to have allias for the font used in certain things, like a button. So I can, with one change, change the font of everything of a particular type.

thanks for your attention!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.