C++ declaring a static object pointer in a class

Viewed 23

I would like to declare an object pointer as static in a class like so:

class sequencer
{
  static HardwareTimer *MyTim;
  public:
  // etc. etc.
}

HardwareTimer sequencer::*MyTim;

The user in this post had a similar issue, with the difference that mine is a pointer to an object where theirs is not.

The format that I used is copied from the format in the linked post, but I am getting the following compiler error:

in function `sequencer::setup()':
main.cpp:(.text._ZN9sequencer5setupEv+0x60): undefined reference to `sequencer::MyTim'

If additional information is needed, this is in the Arduino environment using the stm32duino core. The library that I am using is here. Thanks in advance.

1 Answers

no need to use the static keyword if you want to change the object. you can simply make the object point to another object in Heap

for Example: // HardwareTimer *MyTim = nullptr; // declaration

and if you want to change where the pointer points you can simply do this // *MyTim = new sequencer; and if you want to change what its pointing at again make sure you delete the previous object

// delete MyTim // deletes the pointed at object; then you can point at a new object like above.

p.s: Make sure that the pointer you are declaring is from the parent class

hope this helps best of luck .

Related