What is constructorhtml
默认实参,构造函数,普通函数都有这种性质。ios
ctor's overloadingide
参考:C++——overloading principle analysis函数
关于header file、static、inline、variable hides的一点感想post
咱们知道C不容许overloading,C++容许overloading。C++在编译器一侧时别到的函数名与咱们看到的函数名是有很大差别的。normal function和ctor的overloading方式差很少。下图事normal function的重载。this
注意:黄色部分①②两个ctor则不能够同时存在,①对于的ctor全部形参都有默认参数,这时候就至关于default ctor,和②是同样的效果。编译器在这种状况下没法作出选择。虽然这也是overloading,可是这种overloading让compiler困惑url
常量成员函数spa
在函数后头加const,表示函数不会改变调用者的内容,或 形参在函数内部不会改变。3d
若是忘记加const,右下角的例子,c1是个常量,而real和imag却有可能改变c1,产生矛盾,编译器报错。调试
singleton——把构造函数放在private区
design patrern中 的一种模式
构造函数的做用
①构造对象:在用类定义一个对象时会自动调用构造函数
②初始化对象:类的数据成员每每放在构造函数里面初始化
③类型转换:看讲解
不管C 、C++都是强类型语言,不一样类型变量之间不能随便赋值。不少时候咱们认为理所固然的赋值都是借助临时变量来实现的。看代码
1 #include<iostream> 2 class Test { 3 public: 4 Test(int a=0) 5 { 6 std::cout << "Create Test Object:" << this<<std::endl; 7 this->a = a; 8 } 9 ~Test() 10 { 11 std::cout << "Free Test Object:" << this << std::endl; 12 } 13 private: 14 int a; 15 }; 16 17 int main(int argc, char **argv) 18 { 19 Test t1; 20 t1 = 100; 21 getchar(); 22 return 0; 23 }
调试结果
这里调用了2此构造函数,2次析构函数(析构顺序是栈操做顺序)。输出结果解析
第1行输出:执行第19行,调用构造函数
第2行输出:执行第20行,调用构造函数。这里建立了一个临时Test类型对象(C++为了凸显逼格把类类型的变量称为对象),100就是传入构造函数的参数。
第3行输出:第2行构造完临时对象后,赋值给了对象t1。赋值之后临时对象的生命期就结束了,这里调用析构函数,终结这个临时对象
第4行输出:第22行return 0返回后调用析构函数,终结对象t1
画图解释这一过程
这里面最关键的一个环节就是int类型到Test临时对象转换,int类型要找到一个途径来转换成Test类型。这个途径是什么呢? 构造函数
而咱们代码里面偏偏有这么个构造函数,他的参数就是一个整形。若是咱们干掉这个构造函数,换一个不带参数的构造函数(或者干脆不写构造函数,使用默认的),则不能将100赋值给t1
代码以下
1 #include<iostream> 2 class Test { 3 public: 4 Test() 5 { 6 std::cout << "Create Test Object:" << this << std::endl; 7 this->a = a; 8 } 9 ~Test() 10 { 11 std::cout << "Free Test Object:" << this << std::endl; 12 } 13 private: 14 int a; 15 }; 16 17 int main(int argc, char **argv) 18 { 19 Test t1; 20 t1 = 100; 21 getchar(); 22 return 0; 23 }
1 #include<iostream> 2 class Test { 3 public: 4 ~Test() 5 { 6 std::cout << "Free Test Object:" << this << std::endl; 7 } 8 private: 9 int a; 10 }; 11 12 int main(int argc, char **argv) 13 { 14 Test t1; 15 t1 = 100; 16 getchar(); 17 return 0; 18 }
这种状况直接编译不过。错误 C2679 二进制“ = ”: 没有找到接受“int”类型的右操做数的运算符(或没有可接受的转换)。在类型转换的时候,编译器试图寻找能实现转换的构造函数,能找到就执行构造函数生成一个临时对象好用于赋值。找不到就报错。
额外补充:对于隐式类型转换,生成的临时对象具备const性质。参考 C++——引用
explicit关键字
有的时候咱们会发现构造函数前面加了个关键字explicit,代码以下
1 #include<iostream> 2 class Test { 3 public: 4 explicit Test(int a = 0) 5 { 6 std::cout << "Create Test Object:" << this << std::endl; 7 this->a = a; 8 } 9 ~Test() 10 { 11 std::cout << "Free Test Object:" << this << std::endl; 12 } 13 private: 14 int a; 15 }; 16 17 int main(int argc, char **argv) 18 { 19 Test t1; 20 t1 = (Test)100; 21 getchar(); 22 return 0; 23 }
在进行赋值 或 类型转换的时候,若是要借助构造函数是不容许隐式转换的。因此第20行必须强制转换(显式转换)。