class - How to look at Constructors in OOP -
in oop books , articles, read constructors:
a constructor specialized method in class, name identical class name, , has no return type.
so our constructor of form:
class myclass { public: /* no return type */ myclass(); }
well, definition has conflicts oop principles: methods must have return type (even if void
), , in object, cannot have 2 members same name, method cannot has same name class's name.
i think can change definition one:
a constructor specialized method in class, has no name – anonymous or nameless method – , return type (the type of) class.
so have:
class myclass { public: myclass /* no method name */ (); }
in second definition: allowed have anonymous methods in oop languages, , our method has return type.
var obj = new myclass();
constructor called, , obj
instance of myclass
, constructor has return value. in other hand, new myclass()
contract calling accessible anonymous method on blueprint of object named myclass
.
what think? wrong or can use definition or take perspective looking @ constructors?
update: why getting down , close votes? explain please.
operator new
2 things:
- allocates memory new object (and pointer piece of memory assigned obj in example).
- calls constructor initializes object default values.
so, in sense, constructor doesn't return anything. it's new
operator returns object. constructors works decorator: receives piece of memory , initializes various bytes in memory default values.
it answers first point, constructor must have return type. constructor initializes data allocated new
operator can't return anything, because if did want return expression new myclass()
would, in fact, have return 2 types @ same time!! 1 type object allocated operator new
, second type same object decorated constructor.
if want picky definitions rather suggest changing name method
in description else (maybe decorator, in design pattern?).
and said method must return type? abstract methods, return anything?
edit answer of questions:
yeah, abstract methods have return type don't return anything, methods implements interface define. point show not defined method has return type. methods (or language constructs) return , don't, regardless of how call them.
i guess in case of constructor easier define them in terms of methods don't return anything, because similar other regular method, apart fact don't return anything, rather trying make definition method-like construct don't return anything.
operator new
doesn't create object. it's operator, language construct. when code gets compiled new
ceases exist, it's replaced assembler code allocate , initialize piece of memory represent object.
let's disassemble steps taken when executing new myclass()
1 one:
- operator
new
allocates piece of memory size needed store variables , metadata of classmyclass
parent , child classes. - the operator executes constructors of parent classes of class
myclass
(if any). - the operator executes constructors of child classes of class
myclass
(member variables stored inmyclass
not build-in classes themselves). - the operator executes constructor of class
myclass
. - the operator
new
returns created object (or pointer allocated piece of memory).
very there 1 constructor execute. if class has parent classes , holds custom classes within new
operator has execute few, if not dozens of constructors before being able return object. return types go if each constructor had return something, , point of doing if can't whatever constructors return?
what animal operator?
i think confusing here definition of operator. operator not method invoke. thinking new
says create object wrong saying +
says add 2 objects or =
says compare objects. operators can overloaded principle operators language constructs define operations language (the compiled assemble code) performs, e.g. assign variable sum of 2 objects, create object, etc. see operators on wikipedia.
Comments
Post a Comment