- Home /
Does a Constructor *always* build an Object?
Or can it create some other entity?
--- EDIT ---
Ok, then let me rephrase that:
Can the constructer be called on anything else but on an object?
I guess what I really want to ask is:
Are those things created from Structs real, normal Objects?
(Don't ask me why I didn't ask that in the first place O.o Maybe I thought this way it was a more multi-purpose-question... o. )
__
Does that mean that when the Constructor is called, the system first creates some kind of "generic Object-Thingy" and then this Thing gets processed by the Constructor to imprint on it the traits of the Class/Struct it was called for?
Thanks & Greetz, Ky.
Answer by Bampf · Dec 31, 2009 at 12:04 PM
The constructor is implicitly called as part of the initialization of the object, after it has been allocated but before the object fully exists to be used by the next line of code. (Note that there can be more than one constructor for a particular class, and a constructor of each superclass of the object will already have been called before the subclass's constructor is called.)
If you want to write some code that decides what type of object to create, that code would have to go somewhere else. When it creates an object of the desired type, a constructor of that object will be called as a matter of course.
Structs are indeed objects, although they are different in some respects. See: "What's the difference between a struct and a class?" for more info.
Answer by duck · Dec 30, 2009 at 10:33 PM
The constructor itself doesn't build the object, but it is always called on any object as soon as it is created. This is similar to (but not the same as) the way Awake() is called on new gameobjects.
Note - an 'object' in object oriented programming is not the same as a 'GameObject' in Unity.
Your answer
Follow this Question
Related Questions
Not every Class in the API is derived from the "Object"-Class, so what are they? 2 Answers
What's the difference between a struct and a class? 1 Answer
Does Unity support multiple Inheritance? 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Does Unity support the declaration of several classes in one script? 2 Answers