0

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.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Hitachii
  • 1
  • 1
  • 2
    `HardwareTimer sequencer::*MyTim` -> `HardwareTimer* sequencer::MyTim`. Note the `*` is in a different place. – Miles Budnek Sep 25 '22 at 03:11
  • 1
    `HardwareTimer sequence::*MyTim` defines `MyTim` as a pointer to a non-static member of `sequencer` that is of type `HardwareTimer`. What you need is `HardwareTimer *sequencer::MyTim` which declares `sequence::MyTim` as a pointer to a `HardwareTimer` (which is consistent with the declaration in class `sequencer`). – Peter Sep 25 '22 at 03:19

1 Answers1

0

No need to use the static keyword if you want to change the object. You can simply make the pointer of an object point to another another location in memory in Heap.

For Example:

HardwareTimer *MyTim = nullptr; // declaration of empty pointer.

And if you want to change where the pointer points to, you can simply do this:

*MyTim  = new sequencer;

Make sure you delete the previous object and make sure that the pointer you are declaring is from the parent class.

delete MyTim // deletes the pointer;

  

Then you can point at a new object like above.

Hope this helps Best of luck.

Danilo
  • 1,017
  • 13
  • 32
Omar Azzam
  • 11
  • 1