Initialise a class to a pre existing pointer

Hi, I'm trying to start an object of a class as a pointer like this:

MyClass *ObjectPtr;

but instead of initialising a pointer have a Global pointer pre defined, I don't know if this is even possible?

I mean something like this ( I don't really know )

int* myptr;


void setup()
{
  
    myptr = new MyClass();

}

Any help is really appreciated =)

MyClass *ObjectPtr;

void setup()
{
  ObjectPtr = new MyClass();
  ObjectPtr->someMethod();
}

but instead of initialising a pointer have a Global pointer pre defined, I don't know if this is even possible?

It is.

But, not the way that you have shown. The type of the global pointer MUST be the same type as the local pointer.

Which, of course, makes me question why you need a local pointer at all.

The code you propose is how you value a global pointer (or would be if the type of myptr was correct), which is not what you asked about.

Please do a better job of explaining what you are trying to accomplish.

PaulS:
It is.

But, not the way that you have shown. The type of the global pointer MUST be the same type as the local pointer.

Which, of course, makes me question why you need a local pointer at all.

The code you propose is how you value a global pointer (or would be if the type of myptr was correct), which is not what you asked about.

Please do a better job of explaining what you are trying to accomplish.

I want to be able to assign the global pointer to different classes.

cheche_romo:
I want to be able to assign the global pointer to different classes.

what good would that be?

I think you mean different OBJECTS!

BulldogLowell:
what good would that be?

I think you mean different OBJECTS!

Of course!

my bad, im really new with all the terms. lol :smiley:

So you want a pointer which points to objects which are MyClass objects?

Then you have to give it the type of MyClass*

Then you have to create the object for it to point to. The new operator can do this.

You don't normally need to create pointers to global objects. They're static, so they don't move or get created and destroyed.

MorganS:
You don’t normally need to create pointers to global objects. They’re static, so they don’t move or get created and destroyed.

You may want to move through (iterate over) an array of objects… you’d normally do that with pointers; for example:

#include <Servo.h>
Servo* myServos[3];
byte servoPin[3] = {5,6,7};

void setup() 
{
  byte i = 0;
  for(auto srvo : myServos)
  {
   srvo = new Servo();
   srvo->attach(servoPin[i++]);
   srvo->write(0);
  }
}

MorganS:
So you want a pointer which points to objects which are MyClass objects?

Then you have to give it the type of MyClass*

Then you have to create the object for it to point to. The new operator can do this.

You don't normally need to create pointers to global objects. They're static, so they don't move or get created and destroyed.

Ok so let me be more specific. I want to create an Array of pointers that are capable of being started as different objects of a class.

for example having an array of 8 global pointers, and then being capable of starting each pointer as MyClass1, MyClass2 ,...

int* PtrArray[8];

void setup()
{
    *PtrArray[0] = new MyClass1;
    *PtrArray[1] = new MyClass2;
    *PtrArray[2] = new MyClass3;
    *PtrArray[3] = new MyClass2;
}

I think that the objects should be allocated dynamically. so when you end an object of MyClass1 and create one of MyClass2, you don't have a memory leak.

well only if it is possible, Im doing this to reduce memory consumption, I think I should have told that, my bad. :o

cheche_romo:

int* PtrArray[8];  // <<<<<<this is an array of pointers to int<<<<<<<

well only if it is possible…

have you not read the previous posts?

what makes you think that you can have an int pointer pointing to a class object?

I'm sorry, I just don't quite get it, is there a proper way to approach what I am trying to do?

cheche_romo:
I'm sorry, I just don't quite get it, is there a proper way to approach what I am trying to do?

reply #1

for example having an array of 8 global pointers, and then being capable of starting each pointer as MyClass1, MyClass2 ,...

What, EXACTLY, does "starting a pointer" mean to you? It means NOTHING to me.

"initializing a pointer", on the other hand, DOES.