Skip to main content
Implement in the .h → inline in the class declaration.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

If you want to specify the number of spaces/dashes/... every time you call any of these methods, you just have to make that number a parameter of the method:

void lines(int count)
{
    for (int x = 0; x < count; x++)
        Serial.println();
}

Then, your class does not need its private _counter. Then, since there is no data in the class, you do not need to instantiate it: just make all the methods static and call the methods as nFormat::method_name(). And since you are not making instances, you do not need a constructor either. And since the methods are really trivial, there is no point in implementing them in a .cpp file: you can just implementinline them in the .hclass declaration. Combining all this gives this small demo programdemo program:

class nFormat
{
  public:
    static void lines(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.println();
    }

    static void spaces(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print(" ");
    }

    static void dashes(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print("-");
    }
};

void setup()
{
    Serial.begin(9600);
    nFormat::dashes(5);
    nFormat::lines(5);
    nFormat::spaces(5);
    nFormat::dashes(5);
}

void loop() {}

It could be noted than this class serves only to avoid putting all the functions in the global namespace. It is essentially a namespace. You could as well replace class by namespace (and remove the public keyword) to make that more explicit.

If you want to specify the number of spaces/dashes/... every time you call any of these methods, you just have to make that number a parameter of the method:

void lines(int count)
{
    for (int x = 0; x < count; x++)
        Serial.println();
}

Then, your class does not need its private _counter. Then, since there is no data in the class, you do not need to instantiate it: just make all the methods static and call the methods as nFormat::method_name(). And since you are not making instances, you do not need a constructor either. And since the methods are really trivial, there is no point in implementing them in a .cpp file: you can just implement them in the .h. Combining all this gives this small demo program:

class nFormat
{
  public:
    static void lines(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.println();
    }

    static void spaces(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print(" ");
    }

    static void dashes(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print("-");
    }
};

void setup()
{
    Serial.begin(9600);
    nFormat::dashes(5);
    nFormat::lines(5);
    nFormat::spaces(5);
    nFormat::dashes(5);
}

void loop() {}

It could be noted than this class serves only to avoid putting all the functions in the global namespace. It is essentially a namespace. You could as well replace class by namespace (and remove the public keyword) to make that more explicit.

If you want to specify the number of spaces/dashes/... every time you call any of these methods, you just have to make that number a parameter of the method:

void lines(int count)
{
    for (int x = 0; x < count; x++)
        Serial.println();
}

Then, your class does not need its private _counter. Then, since there is no data in the class, you do not need to instantiate it: just make all the methods static and call the methods as nFormat::method_name(). And since you are not making instances, you do not need a constructor either. And since the methods are really trivial, there is no point in implementing them in a .cpp file: you can just inline them in the class declaration. Combining all this gives this small demo program:

class nFormat
{
  public:
    static void lines(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.println();
    }

    static void spaces(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print(" ");
    }

    static void dashes(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print("-");
    }
};

void setup()
{
    Serial.begin(9600);
    nFormat::dashes(5);
    nFormat::lines(5);
    nFormat::spaces(5);
    nFormat::dashes(5);
}

void loop() {}

It could be noted than this class serves only to avoid putting all the functions in the global namespace. It is essentially a namespace. You could as well replace class by namespace (and remove the public keyword) to make that more explicit.

Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

If you want to specify the number of spaces/dashes/... every time you call any of these methods, you just have to make that number a parameter of the method:

void lines(int count)
{
    for (int x = 0; x < count; x++)
        Serial.println();
}

Then, your class does not need its private _counter. Then, since there is no data in the class, you do not need to instantiate it: just make all the methods static and call the methods as nFormat::method_name(). And since you are not making instances, you do not need a constructor either. And since the methods are really trivial, there is no point in implementing them in a .cpp file: you can just implement them in the .h. Combining all this gives this small demo program:

class nFormat
{
  public:
    static void lines(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.println();
    }

    static void spaces(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print(" ");
    }

    static void dashes(int count)
    {
        for (int x = 0; x < count; x++)
            Serial.print("-");
    }
};

void setup()
{
    Serial.begin(9600);
    nFormat::dashes(5);
    nFormat::lines(5);
    nFormat::spaces(5);
    nFormat::dashes(5);
}

void loop() {}

It could be noted than this class serves only to avoid putting all the functions in the global namespace. It is essentially a namespace. You could as well replace class by namespace (and remove the public keyword) to make that more explicit.