Use class without instance (like e.g. String)

Hi,

I want to develop a class which I can use without building an instance, like String. I want to use the class for conversation, e.g. "Serial.println (dateConvert (ulong unixEpoch).toMyFormat())", the output is as String.

How can I setup such a class?

regards
Frank

Can you give an example of use of String that doesn't build an instance? (Trick question!)

Why do you want to develop a class for this? Why not just develop a function?

What is dateConvert() in your example? What type does it return?

You could develop a class which takes whatever type dateConvert() returns as a constructor parameter and has a toMyFormat() method. But why do that? It's less efficient than a normal function.

I’m not aware of String not having instances… not sure what you mean here.

I suspect that what @franks_pv has in mind is using an object without having to explicitly create an instance of that object. This would be much as the same way that Serial and String work in the Arduino environment

My guess is that OP is looking for static member function.

Putting functions in a class could help to keep things together and help to prevent name collision. It may also improve the ease of reuse.

I agree that on AVR the extra (memory/processing) cost of a class may be a drawback. But on ESP32 it is not that easy to fill all memory... and the extra memory for the class will be negligible...

But together with what? @franks_pv hasn't mentioned anything else that this method would be together with....

Can use namespaces for that

namespace foo {
  int bar() { return 7; }
  constexpr int fiz = 23;
}

void setup() {
  Serial.begin(115200);
  Serial.println(foo::bar() * foo::fiz);
}

void loop() {}

After typing foo:: IDE will even suggest bar and fiz

If there are no member fields, then no point in using class or struct. static member functions are useful for wrangling instances as a whole, but not if there are never any instances.

you are right. The given idea of me is only an example. The Idea is to have such a class that can be used without separate instance inline.

Every class can be used this way

After a class is declared you can create a "working instance" that you will use elsewhere. You can do this directly in .hpp or .cpp file for example:

class myClass {
    public:
        void doSomething () {};  
};

myClass myWorkinInstance;

You can latter use the "working instance" in other modules:

void setup () {    
    myWorkinInstance.doSomething ();
}
myClass myWorkinInstance;

That is what @franks_pv is trying to avoid doing, but I have no idea why

I thought he didn't really know how to express what he wishes.

May be I’m wrong but I think what OP wants to do is to add new types to the print() functions

While in the use case you're thinking of, it may appear that an instance isn't being created, it actually is! The instance is temporary and destroyed after its use in the statement.

As already noted, you can do this with any class.

Example:

class myClass {
  public:
    myClass(uint32_t x) : val(x) {};
    const char *formatPrint() {
      snprintf(stringBuf, maxLength, "Formated: %lu", val);
      return stringBuf;
    }

  private:
    static constexpr size_t maxLength {20};
    uint32_t val;
    char stringBuf[maxLength + 1] {0};
};

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println(myClass(123456).formatPrint());

}

void loop() {
}

Output:

Formated: 123456

That makes not so much sense for me.
You can enrich a class with new functionality.
You will need an instance to apply the functionality.

Please make a real world example what you want to do.

See Post #16.

This is not a real world example from the TO.
I would like to see a real world example from the TO.

It's the answer to OP's question .... a working example of the pattern that OP wants to implement. Whether or not it is "real world", practical, or useful is immaterial to the question and answer.