c ++ object initialization (Weng Kai c ++ class disclosed [10])

c ++ object initialization is to call the constructor initialization operation is completed;

Constructor Constructor whether the parameters, there are parameters constructor, the default constructor (compiler give us achieve) ... (said after the copy constructor)

Note: The default constructor only if we ourselves do not declare the constructor will gave us a no argument constructor compiler, if we define, the compiler will not have control

Example: see below the class C compiler does not give us a default constructor to initialize

#include <the iostream> 

class A // default constructor A () which is at compile time, the compiler to achieve our { }; class B { public: B () {} // no argument constructor }; class C { public: C ( int I) {} // have argument constructor }; class D { public: D () {} // no constructor parameter D ( int I) {} // have argument constructor }; int main () { A a; B b; C C; // error only in the case where no constructor will give us a compiler manufacturing default constructor with no arguments C C1 ( . 1 ); // OK D D, D1 ( 0 ); // OK return 0 ; }

 An error saying: Can not find the constructor C :: C () to initialize the object c

Guess you like

Origin www.cnblogs.com/go-ahead-wsg/p/12203820.html