Quantcast
Channel: Learn with BlocklyProp — Parallax Forums
Viewing all articles
Browse latest Browse all 293

Better use of C++'s "using" keyword

$
0
0
I do quite like Java's import statement. I wish C++ had an equivalent, rather than using a preprocessor to pull in header files. Nonetheless, I feel that "using <namespace>::<class>" is a lot better than no "using" keyword or "using namespace <namespace>". I just found out that this is possible, so I have to share :D

I've just turned this code...
#include <PropWare/PropWare.h>
#include <PropWare/gpio/pin.h>

int main () {
    const PropWare::Pin led1(PropWare::Pin::Mask::P17, PropWare::Pin::Dir::OUT);
    led1.start_hardware_pwm(4);

    const PropWare::Pin led2(PropWare::Pin::Mask::P16, PropWare::Pin::Dir::OUT);
    while (1) {
        led2.toggle();
        waitcnt(CLKFREQ / 4 + CNT);
    }
}

into this:
#include <PropWare/PropWare.h>
#include <PropWare/gpio/pin.h>

using PropWare::Pin;

int main () {
    const Pin led1(Pin::P17, Pin::Dir::OUT);
    led1.start_hardware_pwm(4);

    const Pin led2(Pin::P16, Pin::Dir::OUT);
    while (1) {
        led2.toggle();
        waitcnt(CLKFREQ / 4 + CNT);
    }
}

That's much better! And still much safer than "using namespace PropWare" at the top :)

Viewing all articles
Browse latest Browse all 293

Trending Articles