c++数据结构学习笔记
(1)关于类的构造函数
在构造函数前加上explict关键字 就可以使构造函数只可以显式调用,而不可以隐式调用。
下面的例子说明的很清楚:
classTest1
{
public:
Test1(int n)
{
num=n;
}//普通构造函数
private:
int num;
};
classTest2
{
public:
explicit Test2(int n)
{
num=n;
}//explicit(显式)构造函数
private:
int num;
};
intmain()
{
Test1 t1=12;//隐式调用其构造函数,成功
Test2 t2=12;//编译错误,不能隐式调用其构造函数
Test2 t2(12);//显式调用成功
return0;
}
(2)类的getter和setter方法
一般来说getter方法应该声明为const
假如类有一个int属性的num
int getNum() const
{
return num;
}
void setNum(int n)
{
num=n;
}
(3)