c++ - Can constructor do any hard job other than passing values to members? -


so summarize initialization in c++ , constructor can

  1. initialize primitive types garbage values in memory
  2. call default constructors on members automatically
  3. initialize member objects nontrivial constructors in init list

but cannot init array properly. ref: c++: constructor initializer arrays

my question is: how can meaningful initialization in function body? before entering braces, has been initialized in 1 way or another. braces contain cleaning work, things cout<<"this object constructed!" . ref: use of constructor in c++

what if want preprocess parameter before passing constructor of member object? example:

class foo{ public:     int x;     foo(int y){         x = y;     } };  class bar{ public:     foo foo1;     bar(int y){         int z = supercomplicatedoperation(y);         // , other heavylifting work         foo1 = a(z);     } }; 

the code above not compile because compiler tries init foo1 before entering braces in constructor.

i have seen suggestions on solving problem providing default constructor foo. constructor of bar first init foo1 garbage value, , assign foo1 meaningful in ctor body. solution array initialization in quoted thread. isn't "initializing garbage value" oxymoron? if compiler wants me that, wouldn't complain having no default ctor.

in word, ctor supposed init object in user defined way, don't see way nontrivial init. missing here?

================================================================================

clarifications: askingi aware of bar(int y): foo(supercomplicatedoperation(y)) syntax. question on how nicely. design point of view, believe codes in supercomplicatedoperation() should belong ctors. example, bar takes 2 parameters (int x,int y) initialize. can imagine xy coordinates on plane. foo takes coordinates, different reference frame. need

bar(int x,int y):foo(f1(a,b),f2(a,b)) 

and kind of things ugly if ctor has handful of params. besides, init list looks crowded.

and if ctor body "touches" member objects, means init list not set members desired state, rather default zero-like states(e.g. empty vector). therefore when calling ctor, first memory bar allocated, members objects(subchunks of memory) go through phases:

garbage value/uninitialized
--- init list or default member ctor ---> default "zero-like" state
--- ctor body --------------------------> desired initial state

so initialization supposed 2 step process?

[...] constructor can

  1. initialize primitive types garbage values in memory
  2. call default constructors on members automatically

this default-initialization, applies every member that's not specified in initializer list , not have initializer in class definition.

  1. initialize member objects nontrivial constructors in init list

the member initializer list can initialize primitive members.

but cannot init array properly. ref: c++: constructor initializer arrays

one of answers there noted can done in c++11.

my question is: how can meaningful initialization in function body? before entering braces, has been initialized in 1 way or another. braces contain cleaning work, things cout<<"this object constructed!" . ref: use of constructor in c++

um...no. there's substantial logic in constructor body proper. it's not "cleaning up". true @ time enter constructor body every member initialized in way (which may include "not initialized @ all" primitive types), nothing says can't assign them or otherwise modify them. have if there's sort of circular dependencies. code compute stored value long enough it's far more readable inside constructor body.

the code above not compile because compiler tries init foo1 before entering braces in constructor.

i have seen suggestions on solving problem providing default constructor foo.

well, can : foo1(a(supercomplicatedoperation(y)) in example.

so constructor of bar first init foo1 garbage value, , assign foo1 meaningful in ctor body. solution array initialization in quoted thread. isn't "initializing garbage value" oxymoron? if compiler wants me that, wouldn't complain having no default ctor.

a default ctor means object constructed valid object. default ctor doesn't leave members uninitialized (or "initialized garbage values"). consider default ctor std::vector, had better set internal state represents empty vector!

more fundamentally, job of constructors (default or not) establish object's invariants upon functions operate on object depend. objects may have no invariants, many (for instance, vector's internal pointer had better either valid pointer or null). if have no default constructor, compiler has no way of creating valid object when no argument supplied; can't give "initialized garbage value", because has no way of knowing whether can in fact valid object of type.


Comments