global array in constructor

Hi all,
I am writing an IOManager library, and I want to write a constructor that creates a global array.

what is the semantically correct way to do this?

#ifndef IOManager_h
#define IOManager_h

#include "Arduino.h"


class IOManager
{
	public:
	IOManager(uint8_t elementsInArray);
        
	private:

};

#endif

the .cpp

#include "Arduino.h"
#include "IOManager.h"

IOManager::IOManager(uint8_t numberOfElementsInArray)
{
	// Define array with size numberOfElementsInArray
}

Oops seems I placed the wrong code...

Here we go again...

Sp the question is, is this semantically correct?

#ifndef IOManager_h
#define IOManager_h

#include "Arduino.h"


class IOManager
{
	public:
	IOManager(uint8_t elementsInArray);
        
	private:
	byte _portArray[];
};

#endif
#include "Arduino.h"
#include "IOManager.h"

IOManager::IOManager(uint8_t numberOfElementsInArray)
{
        // Define array with size numberOfElementsInArray
	_portArray[numberOfElementsInArray];
}

What do you mean by global array?

As you have it now, your class member _portArray (note your use of the private keyword) will only be accessible 1) in the same scope of the class object, 2) in the context of the class object and 3) from its class member functions.

Are you looking to create an array that is available to any IOManager object or an array that is truly global?

Are IOManager objects that will be created/destroyed dynamically?

BulldogLowell:
What do you mean by global array?

As you have it now, your class member _portArray (note your use of the private keyword) will only be accessible 1) in the same scope of the class object, 2) in the context of the class object and 3) from its class member functions.

Are you looking to create an array that is available to any IOManager object or an array that is truly global?

I only want them accessible via functions like IOManager.set() and .read So it has to be private. The user is not allowed to address it directly.

BulldogLowell:
Are IOManager objects that will be created/destroyed dynamically?

Yes.
It should be possible to create and destroy dynamically. Say to attach an interrupt to a port.

But in most cases it will be static. (for a domotica system for instance 1 switch turning on and off several lights)

It should be possible to create and destroy dynamically. Say to attach an interrupt to a port.

What the heck does dynamically creating/destroying an array have to do with attaching an interrupt (handler) to a (pin in a) port?

Am I getting it wrong?

If I attach a new interrupt to a port halfway a sketch. I create a new instance or am I mistaking?

If I attach a new interrupt to a port halfway a sketch. I create a new instance

A new instance of what? The interrupt handler is a function. Telling the Arduino to call some function when something happens does not create an instance of anything.

And if that function is wrapped in a class from a library?

My command looks like this

IOHandler io(pin, outputPin1, outputPin2, pointer2Function );

And if that function is wrapped in a class from a library?

That is not a function that you can pass to an interrupt handler. That is code to create an instance of a class, IOHandler, called io, with a bunch of arguments.

You STILL have not explained what you are trying to do. You are still too focused on HOW you should do it.

I am writing an IOManager library, and I want to write a constructor that creates a global array.

So … did you want one golbal array shared by all instances of the class, or one for each class?

If it's one shared by all instances of the glass, then you don't do it in a constructor.

If each instance has it's own array, then it's not global.

PaulMurrayCbr:
So … did you want one golbal array shared by all instances of the class, or one for each class?

If it's one shared by all instances of the glass, then you don't do it in a constructor.

If each instance has it's own array, then it's not global.

Ok I see, I meant global inside the class... Not across all instances.

PaulS:
That is not a function that you can pass to an interrupt handler. That is code to create an instance of a class, IOHandler, called io, with a bunch of arguments.

You STILL have not explained what you are trying to do. You are still too focused on HOW you should do it.

You are right, I am just learning about interrupts and fiddling about with them. I find it all very confusing and haven't found a good tutorial on them yet. In my arduino book they only briefly touch on the subject and i can;t get my head around it. So I wanted to write a library that deals with it.

If you do not understand interrupts, trying to create a class where there can be multiple instances of the class, is NOT the way to learn about interrupts.

Ditch the class idea until you thoroughly understand interrupts, classes, static methods, etc.

The attachInterrupt function needs to know which pin triggers the callback, under what circumstances, and what function to run when the interrupt happens.

Suppose you have a class called Dog, and several instances of the class, called Spot, Fido, and DumbShit.

Suppose that the interrupt is a doorbell ringing.

Suppose that when the doorbell rings, ONE dog is supposed to bark. Which one? How will any given dog know whether to bark, or not?

How does that have anything to do with global (which is the wrong term for what you are talking about; static is the correct term) arrays?

I follow your example completely.
(You should write Christmas stories...)

Still a bit confused about the terminology. See if I got it right this time...

A variable available throughout my entire class is not called global. A variable accessible by all instances of a class is. (I then have to declare it outside my class?)

A static variable means static per instance. Every instance can have a different value and is remembered.

as for the doorbell. What you describe is not possible or is it?
An interupt is attached to a pin. So all dogs would bark, no?

A static variable means static per instance.

No. A static variable, or function, is owned by the class, not any instance of the class.

A variable available throughout my entire class is not called global.

If a variable is static, it is owned by the class, not an instance of the class. If the variable is in the class, but is not static, is it cloned for each instance of the class, and is NOT shared between instances of the class. If the variable is defined outside of the class, it is global, and has NOTHING to do with the class.

int g_pin; // global, not related to the class

class something
{
   public:
      static int s_pin; // Owned by the class, not usable by any instance of the class

      int m_pin; // Cloned for each instance of the class
};

What you describe is not possible or is it?

The class can have a static method, doSomeBarking() that is called when the doorbell rings. The class can have a static method, letMeBark(), that any instance can call, to indicate that it wants to bark. The class can have a static pointer to an instance of the class, set by the letMeBark() function, pointing to the one dog that wants to bark. The class can have a static array of pointers, pointing to each of the dogs that wants to bark.

So, yes, it is possible to have a static method of the class deal with the interrupt, and have a specific instance of the class, or many instance of the class, do something when the interrupt happens.

An interupt is attached to a pin. So all dogs would bark, no?

No. The callback is to a class method, not an instance method. It is up to the class to determine what to do when the interrupt happens. It could make the last dog bark, or, as suggested above, any specific dog, or all dogs. But, the dogs do not bark just because the doorbell rang.

Kevin77:
A variable available throughout my entire class is not called global. A variable accessible by all instances of a class is. (I then have to declare it outside my class?)

A static variable means static per instance. Every instance can have a different value and is remembered.

Variables outside in any class, struct or function is a global variable.

A variable declared static within a class, or structure, has class wide scope and all instance of the class access the same variable/value.

Thanks for the info! Now I at least I have the terminology right!