詳解C++構(gòu)造函數(shù)
1.作用
一種特殊類型的方法,在每次實例化對象時運行
2.代碼舉例
2.1 示例1:
#include <iostream>
class A
{
public:
float a, b;
void print()
{
std::cout << a << " , " << b << std :: endl;
}
};
int main()
{
A a;
a.print();
return 1;
}
運行結(jié)果:

當(dāng)我們實例化A,系統(tǒng)為它分配內(nèi)存,我們沒有初始化內(nèi)存,得到的是內(nèi)存空間原有的那些東西
2.2 示例2:
當(dāng)在main中添加 std::cout << a.a << " , " << a.b << std :: endl;
int main()
{
A a;
std::cout << a.a << " , " << a.b << std :: endl;
a.print();
return 1;
}
(ubuntu下 vs code )運行結(jié)果:

不同編譯器可能不一樣,有的會編譯不過報錯(未初始化局部變量),原因有待深入…
3. 使用
3.1 使用構(gòu)造函數(shù)初始化
#include <iostream>
class A
{
public:
float a, b;
A ()
{
a = 0.0f;
b = 0.0f;
}
void print()
{
std::cout << a << " , " << b << std :: endl;
}
};
int main()
{
A a;
std::cout << a.a << " , " << a.b << std :: endl;
a.print();
return 1;
}
結(jié)果:

3.2 有參數(shù)的構(gòu)造函數(shù)
#include <iostream>
class A
{
public:
float a, b;
// 無參構(gòu)造
A ()
{
a = 0.0f;
b = 0.0f;
}
// 有參構(gòu)造
A(float c,float d)
{
a = c;
b = d;
}
void print()
{
std::cout << a << " , " << b << std :: endl;
}
};
int main()
{
A a(5.0,6.0);
std::cout << a.a << " , " << a.b << std :: endl;
a.print();
return 1;
}
一個類可以有很多構(gòu)造函數(shù) 前提是參數(shù)個數(shù)不同或者參數(shù)類型不同
類似于同名函數(shù)(函數(shù)重載 即有相同的函數(shù)名,但是有不同的參數(shù)個數(shù)與參數(shù)類型)
A(float c,float d)
{
}
A(int c,int d)
{
}
A(float c,float d,float e)
{
}
這里需要注意有參構(gòu)造的時候注意傳值類型
如 float 類型
A a(5.0f , 6.0f);
3.3 默認(rèn)的構(gòu)造函數(shù)
每個類默認(rèn)有一個空參空實體的構(gòu)造函數(shù)(如果寫了構(gòu)造函數(shù),則默認(rèn)構(gòu)造函數(shù)就沒有了,需要時需手動添加)
A ()
{
}
如果不想使用構(gòu)造函數(shù)有兩種方法
// 1 私有化
private :
A(){}
// 2 刪掉
A() = delete;
4. 成員初始化列表
例1:正常初始化
#include <iostream>
using namespace std;
class Student
{
private:
const char *m_name;
int m_age;
float m_score;
public:
// 無參構(gòu)造 給變量賦定值
Student()
{
m_name = "aaa";
m_age = 1;
m_score = 99.0;
}
// 有參構(gòu)造 給變量動態(tài)賦值
Student(const char *name, int age, float score)
{
m_name = name;
m_age = age;
m_score = score;
}
void print ()
{
cout << m_name << " ," << m_age << " ," << m_score << endl;
}
};
int main(int argc, char const *argv[])
{
Student s1;
s1.print();
Student s2("ccc" , 2 , 99.3f);
s2.print();
return 0;
}
例2:成員初始化列表
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
// string m_name;
// char *m_name;
const char *m_name;
int m_age;
float m_score;
public:
// 無參 成員初始化列表
Student()
: m_name("bbb") , m_age(2) , m_score(93.0f)
{
// TODO
}
// 有參 成員初始化列表
/**
* const char *name 常量指針 const 修飾*name *name不可改變
* char * const name 指針常量 const 修飾 name name不可改變
* char const *name 常量指針 等同于 const char *name
*
* 這里不寫const 會報警告 但可以編過
*
*/
Student(const char *name, int age, float score)
: m_name(name) , m_age(age) , m_score(score)
{
// TODO
}
void print ()
{
cout << m_name << " ," << m_age << " ," << m_score << endl;
}
};
int main(int argc, char const *argv[])
{
Student s1;
s1.print();
Student s2("ccc",2,99.3f);
s2.print();
return 0;
}
運行結(jié)果都一樣:
aaa ,1 ,99
ccc ,2 ,99.3
使用構(gòu)造函數(shù)初始化列表并沒有效率上的優(yōu)勢,僅僅是書寫方便,尤其是成員變量較多時,這種寫法非常簡單明了。
初始化列表可以用于全部成員變量,也可以只用于部分成員變量
Student(char *name, int age, float score): m_name(name){
m_age = age;
m_score = score;
}
NOTE:成員變量的初始化順序與初始化列表中列出的變量的順序無關(guān),它只與成員變量在類中聲明的順序有關(guān)。
為啥推薦成員初始化列表的寫法?
#include <iostream>
using namespace std;
class Example
{
public:
Example()
{
cout<< "Create Example" << endl;
}
Example(int x)
{
cout<< "Create Example with " << x << endl;
}
};
class A
{
private:
string m_name;
// 創(chuàng)建了 Example 的無參構(gòu)造 對象
Example m_Example;
public:
A()
{
m_name = "name";
// 創(chuàng)建新的有參構(gòu)造對象覆蓋第一次賦值
m_Example = Example(1);
}
};
int main(int argc, char const *argv[])
{
A a;
return 0;
}
結(jié)果:

A的構(gòu)造函數(shù)換成成員初始化列表的寫法
// A() : m_name ("name"),m_Example(Example(1)) 與下面寫法相同
A() : m_name ("name"),m_Example(1)
{
}
結(jié)果:

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
虛函數(shù)與純虛函數(shù)(C++與Java虛函數(shù)的區(qū)別)的深入分析
本篇文章是對虛函數(shù)與純虛函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
C語言字符函數(shù)isalnum()和iscntrl()詳解
大家好,本篇文章主要講的是C語言字符函數(shù)isalnum()和iscntrl()詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
matlab?GUI指紋識別門禁系統(tǒng)介紹及源碼實現(xiàn)
這篇文章主要為大家介紹了matlab?GUI指紋識別門禁系統(tǒng)的介紹及源碼實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02

