0

I guess this is kinda far-fetched, but basically, I'm trying to create an instance of a class that I need to work on as if it were globally declared but initialize it once I have obtained some data from memory (previously stored data in the microcontroller). It's a project with an ESP8266 MCU that allows you to configure an led display via wifi, and for that my teammates wanted to make use of an already existing library, the problem is that in order for the user to be able to change the number of devices that they're going to use I would need to (from what I can gather, I started learning about classes a few days ago, I'm still in diapers) use the constructor again since that's when you're supposed to provide the number of devices (after that I'm not sure where I could change that variable again, I don't even know exactly what the constructor does because it's only declared in the .h file but then I wasn't able to find where is it that they define the function. There is a variable in the class that seems to have something to do with that but it's private). Originally I thought of simply passing the value to the void loop() and the other functions I use in the code, but that proved to be ineffective given that I also use another library called ESPAsyncwebserver that returns (I think that's how it works) to the void setup() (to all the places where I used 'objectoftheclass.on()' because it makes a callback) function whenever there's an HTTP request so it throws an error saying that the object was not declared in that scope (which is where I'd be creating it in). The other solution I thought of was to copy one object into an already existing global one once I had the data I needed from memory but that also throws an error.

Is there a way to allocate the memory required for a class outside of main (like a global variable) and then make an instance of that class later on but in that space? Or like, maybe a way of not using a constructor for that object until a later point in time (without really using the object at all before I use the constructor)?

I didn't wanna post the code here because it is both messy and unnecessarily lengthy for a post but tell me if it would help because I'm not quite sure if the idea of what I need here is all that clear, thanks.

tstanisl
  • 13,520
  • 2
  • 25
  • 40
  • Allocating via (say) `malloc` would be UB. How about adding an `Initialise` method and calling that when you're ready to? – Paul Sanders Dec 27 '22 at 11:54
  • 1
    You could use a `std::unique_ptr` or a `std::optional`, or basically any wrapper that allows null states (even a `std::vector` with capacity 1 would work). – Nelfeal Dec 27 '22 at 11:56
  • 2
    Or, use placement new. – Sam Varshavchik Dec 27 '22 at 12:00
  • Possible duplicate of https://stackoverflow.com/questions/3763846/what-is-an-in-place-constructor-in-c – Victor Gubin Dec 27 '22 at 12:29
  • Thanks, people, gonna try all those and see. Also thanks for the link, I'll try that one too :) – Some random guy Dec 27 '22 at 13:04
  • I feel kinda stupid now, I tried something before using placement new which was quite literally changing the members of the class to public in the library and then changing them manually and it works just fine. I'm still going to do what you guys said because what I did seems suspicious. – Some random guy Dec 27 '22 at 13:29
  • Well, placement new wasn't exactly the play because for some reason, even though I allocate memory globally, when I construct the object it is still bound to that function alone, so I'm gonna try the other answers :) – Some random guy Dec 27 '22 at 14:21
  • Apologies for the previous comment, it does work, I simply needed to declare the pointer as global and then I can call the constructor whenever I want to create the object (although I guess it does technically already exist before that, I'm not sure how you're supposed to say that). Thanks, people, I'm still going to try the other answers later because I should still know how they work anyway – Some random guy Dec 27 '22 at 22:32

0 Answers0