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:

  1. allocates memory new object (and pointer piece of memory assigned obj in example).
  2. 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:

  1. operator new allocates piece of memory size needed store variables , metadata of class myclass parent , child classes.
  2. the operator executes constructors of parent classes of class myclass (if any).
  3. the operator executes constructors of child classes of class myclass (member variables stored in myclass not build-in classes themselves).
  4. the operator executes constructor of class myclass.
  5. 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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -