#mermaid-svg-zWvicxV2ys9Bp4rU .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .label text{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .node rect,#mermaid-svg-zWvicxV2ys9Bp4rU .node circle,#mermaid-svg-zWvicxV2ys9Bp4rU .node ellipse,#mermaid-svg-zWvicxV2ys9Bp4rU .node polygon,#mermaid-svg-zWvicxV2ys9Bp4rU .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-zWvicxV2ys9Bp4rU .node .label{text-align:center;fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .node.clickable{cursor:pointer}#mermaid-svg-zWvicxV2ys9Bp4rU .arrowheadPath{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-zWvicxV2ys9Bp4rU .flowchart-link{stroke:#333;fill:none}#mermaid-svg-zWvicxV2ys9Bp4rU .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-zWvicxV2ys9Bp4rU .edgeLabel rect{opacity:0.9}#mermaid-svg-zWvicxV2ys9Bp4rU .edgeLabel span{color:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-zWvicxV2ys9Bp4rU .cluster text{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-zWvicxV2ys9Bp4rU .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-zWvicxV2ys9Bp4rU text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-zWvicxV2ys9Bp4rU .actor-line{stroke:grey}#mermaid-svg-zWvicxV2ys9Bp4rU .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-zWvicxV2ys9Bp4rU #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .sequenceNumber{fill:#fff}#mermaid-svg-zWvicxV2ys9Bp4rU #sequencenumber{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU #crosshead path{fill:#333;stroke:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .messageText{fill:#333;stroke:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-zWvicxV2ys9Bp4rU .labelText,#mermaid-svg-zWvicxV2ys9Bp4rU .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-zWvicxV2ys9Bp4rU .loopText,#mermaid-svg-zWvicxV2ys9Bp4rU .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-zWvicxV2ys9Bp4rU .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-zWvicxV2ys9Bp4rU .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-zWvicxV2ys9Bp4rU .noteText,#mermaid-svg-zWvicxV2ys9Bp4rU .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-zWvicxV2ys9Bp4rU .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-zWvicxV2ys9Bp4rU .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-zWvicxV2ys9Bp4rU .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-zWvicxV2ys9Bp4rU .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .section{stroke:none;opacity:0.2}#mermaid-svg-zWvicxV2ys9Bp4rU .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-zWvicxV2ys9Bp4rU .section2{fill:#fff400}#mermaid-svg-zWvicxV2ys9Bp4rU .section1,#mermaid-svg-zWvicxV2ys9Bp4rU .section3{fill:#fff;opacity:0.2}#mermaid-svg-zWvicxV2ys9Bp4rU .sectionTitle0{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .sectionTitle1{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .sectionTitle2{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .sectionTitle3{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-zWvicxV2ys9Bp4rU .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .grid path{stroke-width:0}#mermaid-svg-zWvicxV2ys9Bp4rU .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-zWvicxV2ys9Bp4rU .task{stroke-width:2}#mermaid-svg-zWvicxV2ys9Bp4rU .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .taskText:not([font-size]){font-size:11px}#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-zWvicxV2ys9Bp4rU .task.clickable{cursor:pointer}#mermaid-svg-zWvicxV2ys9Bp4rU .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zWvicxV2ys9Bp4rU .taskText0,#mermaid-svg-zWvicxV2ys9Bp4rU .taskText1,#mermaid-svg-zWvicxV2ys9Bp4rU .taskText2,#mermaid-svg-zWvicxV2ys9Bp4rU .taskText3{fill:#fff}#mermaid-svg-zWvicxV2ys9Bp4rU .task0,#mermaid-svg-zWvicxV2ys9Bp4rU .task1,#mermaid-svg-zWvicxV2ys9Bp4rU .task2,#mermaid-svg-zWvicxV2ys9Bp4rU .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutside0,#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutside2{fill:#000}#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutside1,#mermaid-svg-zWvicxV2ys9Bp4rU .taskTextOutside3{fill:#000}#mermaid-svg-zWvicxV2ys9Bp4rU .active0,#mermaid-svg-zWvicxV2ys9Bp4rU .active1,#mermaid-svg-zWvicxV2ys9Bp4rU .active2,#mermaid-svg-zWvicxV2ys9Bp4rU .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-zWvicxV2ys9Bp4rU .activeText0,#mermaid-svg-zWvicxV2ys9Bp4rU .activeText1,#mermaid-svg-zWvicxV2ys9Bp4rU .activeText2,#mermaid-svg-zWvicxV2ys9Bp4rU .activeText3{fill:#000 !important}#mermaid-svg-zWvicxV2ys9Bp4rU .done0,#mermaid-svg-zWvicxV2ys9Bp4rU .done1,#mermaid-svg-zWvicxV2ys9Bp4rU .done2,#mermaid-svg-zWvicxV2ys9Bp4rU .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-zWvicxV2ys9Bp4rU .doneText0,#mermaid-svg-zWvicxV2ys9Bp4rU .doneText1,#mermaid-svg-zWvicxV2ys9Bp4rU .doneText2,#mermaid-svg-zWvicxV2ys9Bp4rU .doneText3{fill:#000 !important}#mermaid-svg-zWvicxV2ys9Bp4rU .crit0,#mermaid-svg-zWvicxV2ys9Bp4rU .crit1,#mermaid-svg-zWvicxV2ys9Bp4rU .crit2,#mermaid-svg-zWvicxV2ys9Bp4rU .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-zWvicxV2ys9Bp4rU .activeCrit0,#mermaid-svg-zWvicxV2ys9Bp4rU .activeCrit1,#mermaid-svg-zWvicxV2ys9Bp4rU .activeCrit2,#mermaid-svg-zWvicxV2ys9Bp4rU .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-zWvicxV2ys9Bp4rU .doneCrit0,#mermaid-svg-zWvicxV2ys9Bp4rU .doneCrit1,#mermaid-svg-zWvicxV2ys9Bp4rU .doneCrit2,#mermaid-svg-zWvicxV2ys9Bp4rU .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-zWvicxV2ys9Bp4rU .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-zWvicxV2ys9Bp4rU .milestoneText{font-style:italic}#mermaid-svg-zWvicxV2ys9Bp4rU .doneCritText0,#mermaid-svg-zWvicxV2ys9Bp4rU .doneCritText1,#mermaid-svg-zWvicxV2ys9Bp4rU .doneCritText2,#mermaid-svg-zWvicxV2ys9Bp4rU .doneCritText3{fill:#000 !important}#mermaid-svg-zWvicxV2ys9Bp4rU .activeCritText0,#mermaid-svg-zWvicxV2ys9Bp4rU .activeCritText1,#mermaid-svg-zWvicxV2ys9Bp4rU .activeCritText2,#mermaid-svg-zWvicxV2ys9Bp4rU .activeCritText3{fill:#000 !important}#mermaid-svg-zWvicxV2ys9Bp4rU .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-zWvicxV2ys9Bp4rU g.classGroup text .title{font-weight:bolder}#mermaid-svg-zWvicxV2ys9Bp4rU g.clickable{cursor:pointer}#mermaid-svg-zWvicxV2ys9Bp4rU g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-zWvicxV2ys9Bp4rU g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-zWvicxV2ys9Bp4rU .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-zWvicxV2ys9Bp4rU .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-zWvicxV2ys9Bp4rU .dashed-line{stroke-dasharray:3}#mermaid-svg-zWvicxV2ys9Bp4rU #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU .commit-id,#mermaid-svg-zWvicxV2ys9Bp4rU .commit-msg,#mermaid-svg-zWvicxV2ys9Bp4rU .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-zWvicxV2ys9Bp4rU g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-zWvicxV2ys9Bp4rU g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-zWvicxV2ys9Bp4rU g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-zWvicxV2ys9Bp4rU .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-zWvicxV2ys9Bp4rU .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-zWvicxV2ys9Bp4rU .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-zWvicxV2ys9Bp4rU .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-zWvicxV2ys9Bp4rU .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-zWvicxV2ys9Bp4rU .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-zWvicxV2ys9Bp4rU .edgeLabel text{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zWvicxV2ys9Bp4rU .node circle.state-start{fill:black;stroke:black}#mermaid-svg-zWvicxV2ys9Bp4rU .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-zWvicxV2ys9Bp4rU #statediagram-barbEnd{fill:#9370db}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-state .divider{stroke:#9370db}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-zWvicxV2ys9Bp4rU .note-edge{stroke-dasharray:5}#mermaid-svg-zWvicxV2ys9Bp4rU .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-zWvicxV2ys9Bp4rU .error-icon{fill:#522}#mermaid-svg-zWvicxV2ys9Bp4rU .error-text{fill:#522;stroke:#522}#mermaid-svg-zWvicxV2ys9Bp4rU .edge-thickness-normal{stroke-width:2px}#mermaid-svg-zWvicxV2ys9Bp4rU .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-zWvicxV2ys9Bp4rU .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-zWvicxV2ys9Bp4rU .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-zWvicxV2ys9Bp4rU .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-zWvicxV2ys9Bp4rU .marker{fill:#333}#mermaid-svg-zWvicxV2ys9Bp4rU .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-zWvicxV2ys9Bp4rU {
color: rgba(0, 0, 0, 0.75);
font: ;
}
1.1 为什么是构造函数?
构造函数
自定义构造函数
没有参数
自定义默认构造函数
构造函数默认实参均有定义
默认构造函数
一个参数
可以隐式转换构造函数
编译器合成默认构造函数
类只是一个抽象,需要将其实例化。
1.2构造函数定义一个或者多个,用于控制对象初始化的特殊成员函数。
1.3 构造函数声明构造函数可以初始化非static数据成员。构造函数声明满足三条件:
- 函数名等于类名
- 没有返回值
- 不能是const(扩展知识点:const成员函数)
构造函数之所以不能为const成员函数,是因为构造函数是需要初始化数据成员的,如果声明为const就没法通过this指针来初始化状态了,索性就把这种可能性扼杀了。
1.4 构造函数初始化构造函数初始化列表是用于初始化数据成员的。它位于构造函数声明与定义之间,列表可能只负责部分数据成员的初值给定,对于未给定的初值的成员
- 使用类内初值
- 若仍未初始化则默认初始化(扩展知识点:默认初始化)
类用户使用构造函数构建对象,根据参数不同选择不同的构造函数。
1.6 编译器合成的默认构造函数一个类没有自定义构造函数,编译器将会生成;声明了一个自定义构造函数,编译器则不会为你做任何构造函数,除非你用了default关键字
默认构造函数能初始化一些STL库,但是不能初始化内置类型和复合类型
注:内置类型就是基础的数据元素,如float int ,复合类型是数组指针之类的。
其他知识 类成员声明顺序应与初始化列表顺序一致注意:类中声明决定初始化顺序,初始化列表的顺序没有任何意义。
class X
{
int i;
int j;
public:
X(int val):j(val),i(j){};
}
类中的声明顺序是先i后j,构造函数初始化首先使用j初始化i,显然是错误的。
委托构造函数减少重复初始化列表委托构造函数用大白话说就是“利用一个比较完整的构造函数来完成一些,部分构造函数”
class A
{
int a;
int b;
int c;
int d;
string str;
public:
A(int p_a,int p_b,int p_c,int p_d,string str):a(p_a),b(p_b),c(p_c),d(p_d),str(p_str){{};//一个比较完整的构造函数(完成了所有初始化成员操作)
A():A(1,2,3,4,5,"");//默认构造函数
A(int a,int d):A(a,2,3,d,"apple");
}