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

**URL:** <https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010>\
**Category:** Programming\
**Created:** [June 1, 2025, 10:28am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010 "2025-06-01T10:28:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![franks\_pv](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/franks_pv/32/1126542_2.png) [@franks\_pv](https://forum.arduino.cc/u/franks_pv)\
**Post date:** [June 1, 2025, 10:28am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/1 "2025-06-01T10:28:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [June 1, 2025, 12:08pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/2 "2025-06-01T12:08:02Z")

</div>

> [@franks\_pv](#):
>
> without building an instance, like String

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.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [June 1, 2025, 4:44pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/3 "2025-06-01T16:44:29Z")

</div>

> [@franks\_pv](#):
>
> without building an instance, like String

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 1, 2025, 5:17pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/4 "2025-06-01T17:17:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [June 1, 2025, 6:00pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/5 "2025-06-01T18:00:05Z")

</div>

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

> **[15.7 — Static member functions – Learn C++](https://www.learncpp.com/cpp-tutorial/static-member-functions/)**

> **[C++ Static Members](https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm)**
>
> C++ Static Members - Learn about static members in C++, including static data members and static member functions, and their significance in object-oriented programming.

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [June 1, 2025, 6:14pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/6 "2025-06-01T18:14:11Z")

</div>

> [@PaulRB](#):
>
> Why do you want to develop a class for this? Why not just develop a 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...

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [June 1, 2025, 6:19pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/7 "2025-06-01T18:19:13Z")

</div>

> [@build\_1971](#):
>
> Putting functions in a class could help to keep things together

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

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [June 1, 2025, 7:06pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/8 "2025-06-01T19:06:29Z")

</div>

> [@build\_1971](#):
>
> help to keep things together and help to prevent name collision. It may also improve the ease of reuse.

Can use namespaces for that

```cpp
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.

---

<div class="post-metadata">

**Author:** ![franks\_pv](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/franks_pv/32/1126542_2.png) [@franks\_pv](https://forum.arduino.cc/u/franks_pv)\
**Post date:** [June 2, 2025, 8:21am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/9 "2025-06-02T08:21:51Z")

</div>

> [@UKHeliBob](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [June 2, 2025, 8:38am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/10 "2025-06-02T08:38:53Z")

</div>

Every class can be used this way

---

<div class="post-metadata">

**Author:** ![bojan\_jurca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bojan_jurca/32/1199504_2.png) [@bojan\_jurca](https://forum.arduino.cc/u/bojan_jurca)\
**Post date:** [June 2, 2025, 8:50am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/11 "2025-06-02T08:50:13Z")

</div>

> [@franks\_pv](#):
>
> 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.

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:

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

myClass myWorkinInstance;

```

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

```arduino
void setup () {    
    myWorkinInstance.doSomething ();
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 2, 2025, 9:14am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/12 "2025-06-02T09:14:18Z")

</div>

```arduino
myClass myWorkinInstance;

```

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

---

<div class="post-metadata">

**Author:** ![bojan\_jurca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bojan_jurca/32/1199504_2.png) [@bojan\_jurca](https://forum.arduino.cc/u/bojan_jurca)\
**Post date:** [June 2, 2025, 11:26am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/13 "2025-06-02T11:26:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [June 2, 2025, 11:33am UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/14 "2025-06-02T11:33:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [June 2, 2025, 12:07pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/15 "2025-06-02T12:07:27Z")

</div>

> [@franks\_pv](#):
>
> The Idea is to have such a class that can be used without separate instance inline.

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.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [June 2, 2025, 12:22pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/16 "2025-06-02T12:22:54Z")

</div>

> [@gfvalvo](#):
>
> > [@franks\_pv](#):
> >
> > The Idea is to have such a class that can be used without separate instance inline.
> 
> While in the use case you're thinking of, it my **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:**

```arduino
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:**

```arduino
Formated: 123456

```

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [June 2, 2025, 12:23pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/17 "2025-06-02T12:23:28Z")

</div>

> [@franks\_pv](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [June 2, 2025, 12:24pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/18 "2025-06-02T12:24:05Z")

</div>

> [@noiasca](#):
>
> Please make a real world example what you want to do.

See [Post #16](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/16).

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [June 2, 2025, 12:28pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/19 "2025-06-02T12:28:29Z")

</div>

> [@gfvalvo](#):
>
> See

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

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [June 2, 2025, 12:33pm UTC](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010/20 "2025-06-02T12:33:29Z")

</div>

> [@noiasca](#):
>
> This is not 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.

[Next page](https://forum.arduino.cc/t/use-class-without-instance-like-e-g-string/1386010.md?page=2)
