Ed from TX
Member
Hi everyone,
I'm doing my first embedded project and I noticed the practice of programming mostly in C and scoping things out by using static declarations inside the cpp file. That was a bit unusual to my eyes.
I was thinking of still using namespaces but making the implementation with static classes declared and implemented within a .cpp, not in the .h which seems a bit nicer (to my eyes) instead of the C-style programming. Given that they would be within my namespace that should limit link collisions even if I do not use anonymous namespaces. The static methods could still be used as callbacks.
Something like this:
In foo.h (same as in "C with Namespaces")
In foo.cpp
In main.cpp or main.ino (same as in "C with Namespaces")
Being a noob to embedded systems, and only doing this as a hobby, I was wondering over the pros/cons of these and perhaps other approaches. The free-floating functions kind of induce me seizures LOL
I'm not writing libraries. I'm using Clion with PlatformIO instead of the Arduino UI... if it makes a difference.
What style do you use?
I'm doing my first embedded project and I noticed the practice of programming mostly in C and scoping things out by using static declarations inside the cpp file. That was a bit unusual to my eyes.
I was thinking of still using namespaces but making the implementation with static classes declared and implemented within a .cpp, not in the .h which seems a bit nicer (to my eyes) instead of the C-style programming. Given that they would be within my namespace that should limit link collisions even if I do not use anonymous namespaces. The static methods could still be used as callbacks.
Something like this:
In foo.h (same as in "C with Namespaces")
Code:
namespace Foo {
void foobar();
}
In foo.cpp
Code:
namespace Foo {
class fooImp {
static void foobar() { ... blah, blah... }
<...rest of the implementation...>
}
void foobar() {
fooImp::foobar();
}
}
In main.cpp or main.ino (same as in "C with Namespaces")
Code:
Foo::foobar();
Being a noob to embedded systems, and only doing this as a hobby, I was wondering over the pros/cons of these and perhaps other approaches. The free-floating functions kind of induce me seizures LOL
I'm not writing libraries. I'm using Clion with PlatformIO instead of the Arduino UI... if it makes a difference.
What style do you use?
Last edited: