# faster object instantation

**URL:** <https://forum.arduino.cc/t/faster-object-instantation/538499>\
**Category:** Programming\
**Created:** [July 26, 2018, 3:09pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499 "2018-07-26T15:09:40Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 26, 2018, 3:09pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/1 "2018-07-26T15:09:40Z")

</div>

Hi there. if i created a function like the following and i wanted to create multiple instances of it i believe i would have to create a class for it. is there any way that i could instate an object without having to set up the constructor and also execute the methods separately? so i could just say for example printTimed(time,data); throughout my code? i am thinking that perhaps this might have something to do with making ones own library if it is possible at all. thanks.

```cpp
void printTimed(int timingVal, int data){
  static unsigned long timer = millis();
  if (millis() - timer > timingVal){
    timer = millis();

    Serial.println(data);
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 26, 2018, 3:17pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/2 "2018-07-26T15:17:06Z")

</div>

> is there any way that i could instate an object without having to set up the constructor

You don't NEED to define a constructor. If you don't, the compiler will create one for you that does nothing.

> and also execute the methods separately?

I don't understand this part. Separate from what?

> so i could just say for example printTimed(time,data); throughout my code?

If you make printTimed() a class method, you need an instance to call the method on. That is how the compiler knows WHICH printTimed() function to call.

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 26, 2018, 3:35pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/3 "2018-07-26T15:35:45Z")

</div>

i tried as follows with no constructor and get the error

```cpp
 error: 'void PrintTimed::run(int, int)' is private

```

i tried making the method public and my objects no longer run as separate instances.

```cpp
class PrintTimed {

    void run(int timingVal, int data) {
      static unsigned long timer = millis();
      if (millis() - timer > timingVal) {
        timer = millis();

        Serial.println(data);
      }
    }
};

PrintTimed tester;

void setup() {
  Serial.begin(9600);
}

void loop() {
  tester.run(1000,5);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 26, 2018, 3:39pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/4 "2018-07-26T15:39:35Z")

</div>

> i tried making the method publicand my objects no longer run as separate instances.

You don't think we need to see the code that doesn't work to tell you why it doesn't work?

You have more confidence in us that I do.

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 26, 2018, 3:49pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/6 "2018-07-26T15:49:27Z")

</div>

> Delta\_G:  
> So the question is, why do you think you need a class here? You most likely don't.

i have tried using a function but because the timer data is changed it can only work for a single instance.

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 26, 2018, 3:52pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/7 "2018-07-26T15:52:14Z")

</div>

> PaulS:  
> You don't think we need to see the code that doesn't work to tell you why it doesn't work?
> 
> You have more confidence in us that I do.

the other code i tried without a constructor other than in the method call. works as if i was calling a single function. (only data from one instance is displayed)

```arduino
class PrintTimed {

    public:
    void run(int timingVal, int data) {
      static unsigned long timer = millis();
      if (millis() - timer > timingVal) {
        timer = millis();

        Serial.println(data);
      }
    }
};

PrintTimed tester;
PrintTimed tester1;

void setup() {
  Serial.begin(9600);
}

void loop() {
  tester.run(1000,5);
  tester.run(2000,10);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 26, 2018, 4:17pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/9 "2018-07-26T16:17:16Z")

</div>

> Should timer be a member variable of your class? Is that what you're missing?

Note that Delta\_G is asking about making that a class member, not a variable local to the function.

The reason for making it static, in the function, was to avoid making it global AND to keeo it from being re-initialized every time the function was called. Making it a member variable keeps it from being global and keeps it from being reset each time the function is called.

Though you will need to determine an appropriate point to initialize/reinitialize it. The initialization could be done in the constructor you didn't want to create, but it really should be done in a begin() method, so that you have control over when the timing starts.

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 26, 2018, 4:42pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/10 "2018-07-26T16:42:25Z")

</div>

thanks. this seems to work...

```cpp
class PrintTimed {

    private:
    unsigned long timer = millis();
    int timingVal;
    int data;

    public:
    void run(int timingVal_, int data_) {
      timingVal = timingVal_;
      data = data_;
      
      if (millis() - timer > timingVal) {
        timer = millis();

        Serial.println(data);
      }
    }
};

PrintTimed tester;
PrintTimed tester1;

void setup() {
  Serial.begin(9600);
}

void loop() {
  tester.run(1000,5);
  tester1.run(2000,10);
}

```

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 26, 2018, 4:57pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/12 "2018-07-26T16:57:32Z")

</div>

> Delta\_G:  
> If timingVal and data are coming in with the function call then you don't need member variables to hold that information. This is a waste of four bytes per instance. Only timer really needs to be a member here unless you plan to send in timingVal and data in a constructor and not in the run() method.

right you are, thanks for that! i would also very much like to know whether its possible to give the method arbitrary data types so that the class is more versatile so i could print a string for example or would i be better off just creating another method do you think?

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 26, 2018, 5:30pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/14 "2018-07-26T17:30:35Z")

</div>

> Delta\_G:  
> The search term you want is "function template"

smashing thanks, i will look into these.

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 27, 2018, 9:25am UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/15 "2018-07-27T09:25:44Z")

</div>

> Delta\_G:  
> The search term you want is "function template"

Hi ive just been researching them and have yet to get them to work. i have put together some code as i thought it should be but it does not compile. there are 2 functions. one 'normal one' and one which uses a template. the normal one works as it should but the template version does not. would it be possible to point me in the right direction please? thanks.

```arduino
void myfunc(int x, String y) { //normal function
  Serial.println(x);
  Serial.println(y);
}

template <typename type> void  type myfunc_template( type x, type y) { //template function
  Serial.println(x);
  Serial.println(y);
}

void setup() {
  Serial.begin(9600);
//  myfunc(5, "hi");  
  myfunc_template(5,"hi");
}

void loop() {

}

```

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 27, 2018, 9:36am UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/16 "2018-07-27T09:36:05Z")

</div>

ah, this seems to work...

```cpp
 template <typename type, typename type2> type myfunc_template( type x , type2 y) { //template function, two data types 
  Serial.println(x);
  Serial.println(y);
}

void setup() {
  Serial.begin(9600);

  myfunc_template(5, "hi");
}

void loop() {

}

```

the bit which is confusing me now is that i originally tried declaring the function as void but this didnt work. now i have used the 'type' name it does work. does it matter which of the typenames i use as the functions output data type?

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [July 27, 2018, 9:51am UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/17 "2018-07-27T09:51:28Z")

</div>

You are calling the method in a wrong way:

```cpp
template <class T1, class T2> void templateMethod(T1 arg1, T2 arg2)
{
  Serial.println(arg1);
  Serial.println(arg2);
}

void loop()
{
  //You need to tell the compiler about the wanted data types with <X, Y>
  templateMethod<char*, byte>("cstring", someByte);
}

```

---

<div class="post-metadata">

**Author:** ![gonadgranny](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gonadgranny](https://forum.arduino.cc/u/gonadgranny)\
**Post date:** [July 27, 2018, 11:45am UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/18 "2018-07-27T11:45:51Z")

</div>

> Danois90:  
> You are calling the method in a wrong way:
> 
> ```arduino
> template <class T1, class T2> void templateMethod(T1 arg1, T2 arg2)
> 
> ```

{  
&nbsp; Serial.println(arg1);  
&nbsp; Serial.println(arg2);  
}

void loop()  
{  
&nbsp; //You need to tell the compiler about the wanted data types with \<X, Y\>  
&nbsp; templateMethod\<char\*, byte\>("cstring", someByte);  
}

> ```arduino
> 
> ```

ah ok. could the way i was doing it cause any problems? thanks.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:18pm UTC](https://forum.arduino.cc/t/faster-object-instantation/538499/19 "2021-05-05T23:18:19Z")

</div>


