c++ - why can't objects be created without a constructor? -


why creation of object needs constructor? if don't define constructor default constructor generated ..but why constructor necessary?

why can't objects created without constructor?

this more of discussion on terminology real argument behavior. mention, in cases there nothing done during construction, there should no need constructor, right? well, there concept of trivial-constructor constructor not @ all. sake of standard document, easier treat having (possibly trivial) constructor have provide cases , exceptions in places states 'constructor'.

consider every use of 'constructor' have replaced 'constructor or nothing if type not have virtual functions or bases , no members require generation of constructor'.

this same reason why virtual functions called overrider first 1 in hierarchy definition not override anything. form of generalization makes language easier interpret, although not many people claim section 8.5 on initialization simple...

also note that, while user defined types definition have constructor, constructor not required creation of objects. in particular objects trivial-constructor lifetime starts when memory object allocated (the standard, knowing trivial means nothing done not go through hop of requiring constructor run in case.


3.8 [basic.life]/1

the lifetime of object runtime property of object. object said have non-trivial initialization if of class or aggregate type , or 1 of members initialized constructor other trivial default constructor. [ note: initialization trivial copy/move constructor non-trivial initialization. — end note ] lifetime of object of type t begins when:

-- storage proper alignment , size type t obtained, and

-- if object has non-trivial initialization, initialization complete.

that second bullet used read (c++03): if t class type non-trivial constructor (12.1), constructor call has completed. more stated constructor need not executed. new wording expresses intent in same way. if object has non-trivial initialization, initialization needs complete. objects trivial-constructor (trivial initialization) allocating storage creates object. matter?

struct t { int x; }; // implicitly defined trivial-constructor t *p = static_cast<t*>(malloc(sizeof *p));  // *p alive @ point, no need t *q; { void *tmp = malloc(sizeof t);   q = new (tmp) t;              // call constructor } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -