Go Down

Topic: Declaring Class Variables / Calling Constructors (Read 3 times) previous topic - next topic

tamberg

Hi,

(coming from Java, no C/C++ experience)

there seems to be a difference between Processing and Arduino when it comes to calling a constructor to instantiate an object of a class.

E.g. Processing

   C c; // declare variable c
   c = new C(); // create object of class C
   c.M(); // call method M of c

vs. Arduino

   C c(); // declare & create
   c.M(); call method M of c

Now my questions:

1) Is there a way in Arduino to declare a variable of a class and *later* assign an instance to it?

2) Does Arduino really have classes, objects and constructors?

Regards,
tamberg

PaulS

Quote
1) Is there a way in Arduino to declare a variable of a class and *later* assign an instance to it?


No. What you have to do is move the logic that you would put into a constructor into an initialization method, instead (usually called begin).

Quote
2) Does Arduino really have classes, objects and constructors?


Yes. It has objects, but not instances.

tamberg

Thanks for your quick answer. What do you mean by "objects but not instances"? Isn't an object just an instance of a class? Or are Arduino objects rather like structs in C?

Regards,
tamberg

Spinlock

I have to disagree with PaulS.  It does have objects and instances - just not dynamic instances.

that is to say you can't create and delete objects on the fly using C++'s new and delete.

PaulS

Quote
that is to say you can't create and delete objects on the fly using C++'s new and delete.


To me, that's what distinguishes objects from instances. Objects are statically defined, while instances are dynamically defined.

Perhaps not technically accurate (certainly not with using the word static there), but that's the way I think of objects and instances.

Go Up