What may I do in a constructor and what can't I do and need to create a method f


I hardly know what I am asking about here but it appears to me as often libraries found online are used by first instantiating an object in the global scope and then in the setup() you use a method in that object usually called begin().

Why is that?

I can't point to specific examples but sometimes I have had troubles when trying to get everything done in the constructor body only to find out that it doesn't work but when I move parts of that code into another method function to be called in the setup(), then the functioning of the class suddenly works but I am just confused as to when and why I can do what and what I can't do in the constructor body. And reading up on basic C++ haven't revealed any answers.

If anyone could shed some light on whatever it is that I am writing about I would really appreciate it.

By the way I know about how to initialize objects in another objects constructor through that list(":") thing.


it appears to me as often libraries found online are used by first instantiating an object in the global scope and then in the setup() you use a method in that object usually called begin().

Why is that?

I think it is because the order in which global objects are constructed is not under your control. Any global object you create can't rely on any other global object being constructed. You also can't call any Arduino library functions until the Arduino environment is initialized, which happens just before 'setup()' is called.

The .begin() functions let the libraries initialize anything that references Arduino libraries or global objects.

EXACTLY what johnwasser said. ^ You can set up things in "your own house" but nothing outside is reliable until "start()" begins. I've was bit by this a few times before I learned my lesson.

-jim lee

I see, that fits perfectly with what I have experienced.

Suppose that I had written a lot of code relying on the constructor to use Arduino functions, or suppose I have only written a little code but simply refuse to make my code bloated(to the reader of the code) by adding a bunch of begin() functions, what would be a work-around? is there a work-around?

I have recently been researching how to setup Eclipse to use that for developing code for Arduino and in those hours of reading I also came across someone saying that you can choose to not use setup() and loop() altogether by having a sketch folder as usual contains a .ino file with the same name as the folder, but in that .ino file all we put is a bunch of #include statements that imports all libraries we will be using as well as the .cpp files that also is in the folder and that contains the actual code to be run.

The .ino file have to be there and it has to include all the libraries that other files in the folder is using(or so I have heard) but as long as that is true then we can write our own .cpp file completely neglecting setup() and loop() though the function-name of 'void main()' is already in use by the Arduino core but apart from that we should be able to write a purely C++ program and have it run on the Arduino.

If this is true then this might be a work-around for this problem.

Yes, the workaround is to have a begin() function in each class that requires it.

You could do that but it would be a bad idea. setup() and loop() cover 99.999% of all projects that you might be using an Arduino for.

I think you can override the main() function without using Eclipse and the other steps you mentioned. I think it is a bad idea. I've never tried.

What everyone else said.

In general, the constructor is a place to set an object to a valid internal state. Often, that means just setting a variable to "INIT" and that's something you don't need a constructor for. But occasionally it's a bit more complex.

For instance, you might have variables in your object for "host", "port", and "uri" which are constant over the lifetime of the object. When you declare the object you give it a full url string. A constructor is a reasonable place to parse that string into its components.

In many cases in MPU programming, a class can be instantiated only once, say it uses a specific hardware feature like timer1, and in these cases it could make sense to use a C++ namespace instead which provides the same level of encapsulation of its members as a class would. However, if you are creating say buttons on a touch screen then you need a class of which each button is an instance.
You also mentioned Eclipse. This does not force you to abandon the Arduino setup() / loop() paradigm. Look at Sloeber which is an Arduino Eclipse plugin.

Also, the limitations on constructors are NOT due to the arduino IDE; they’re inherent to the way the C++ compiler is set up on most micro controllers.