Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • You can define your own main() in the sketch if you prefer. This is what the stock one looks like: github.com/arduino/Arduino/blob/1.8.3/hardware/arduino/avr/… Commented Jul 12, 2017 at 14:05
  • 1
    A singleton is effectively a global, just dressed up in an extra confusing way. It has the same downsides. Commented Jul 12, 2017 at 14:05
  • @patstew Do you mind explaining me how you feel it has the same downsides? In my opinion it doesn't since you're able to use data encapsulation to your benefit. Commented Jul 12, 2017 at 15:23
  • @per1234 Thanks! I'm definitely no Arduino expert, but I suppose my first suggestion could work also then. Commented Jul 12, 2017 at 15:28
  • 2
    Well, it's still global state that can be accessed anywhere in the program, you just access it through Program::instance().setup() instead of globalProgram.setup(). Putting related global variables into one class/struct/namespace can be beneficial, especially if they're only needed by a couple of related functions, but the singleton pattern doesn't add anything. In other words static Program p; has global storage and static Program& instance() has global access, which amounts to the same as simply Program globalProgram;. Commented Jul 12, 2017 at 15:32