Object Instantiation Problem


I'm new to C++ and arduino. I've created a custom class, the problem is that its constructors are defined by user, and I must instantiate it after user give the inputs.

In code:

Class dgl2; void somefunction() { ... dgl2 = Class(pins2, false); }

However, the above does not compile, since in "Class dgl2;" I'm not using the correct parameters (int*, bool).

Any suggestions would be very helpful.

Thanks in advance.

The best solution is to move the initialization stuff out of the constructor. Put it in a method. Something like Init().

Then, when you have the needed data, call the Init() method of the object.

By the way, Class is a lousy name for a class.

In you code snippet, dlg2 is an object. The constructor for a class does not return a value, so you can't assign it to an object. Even if it did return a value, you couldn't assign it to an object.

What you seem to be trying to do should be done more like this:

Class *dgl2;
dgl2 = new Class(pins2, false);

That won't work on the Arduino, though, because the new operator is not defined/supported.

You'll also need to make sure that the Init() method takes parameters that you CAN pass to it. Why does your constructor expect an int * now?

Instead of initializing the object with a constructor, provide an initialization function. The Arduino folks like naming initialization methods begin. Your code will look something like this...

Class dgl2;

void somefunction()
  dgl2.begin( pins2, false );

Thanks for all replies.

I've renamed my class, its name is not "Class", it is 'MatrixLED', sorry for that.

The int* parameter is just another way to write int[], in fact, it's just a way I prefer to work with.

Yeah, I've also tried using new keyword, as said, it haven't worked, but did not know that it was not supported.

I'm going to use your suggestions, i knew i was missing something.

Really appreciate that, thanks.