1,可以形成链状的委托构造函数,但是不可以形成委托环:
class weipai2
{
private:
string name;
int sex;
weipai2(string name,int sex):name(name),sex(sex){}
public:
weipai2(string name):weipai2(name,1){}
weipai2(int sex):weipai2("ch",sex){}
weipai2():weipai2(1){}
};
<span style="font-family: Arial, Helvetica, sans-serif;">以下是我的测试代码:</span>
</span><pre name="code" class="cpp"><span style="font-size:18px;">#include <iostream>
using namespace std;
#include<vector>
#include<list>
#include<deque>
class base
{
private:
int i;
public :
base(int i):i(i){}
void set_value(int i)
{
this->i = i;
}
virtual int get()
{
return i;
}
};
class son1 :public base
{
private:
double mf_value;
public:
using base::base;
// using base::set_value; //注释了:调用的是子类函数,没注释:调用基类
void set_value(double mf_value)
{
this->mf_value = mf_value;
}
};
class weipai
{
private:
int name;
int type;
void set_value()
{
name = 3;
cout<<"a"<<endl;
}
public:
weipai(){set_value();}
weipai(int i):weipai() //在此不可以有初始化列表,初始化必须放在函数体
{
cout<<"b"<<endl;
type = i;
}
void put_value()
{
cout<<name<<" "<<type<<endl;
}
};
class weipai2
{
private:
string name;
int sex;
weipai2(string name,int sex):name(name),sex(sex){}
public:
weipai2(string name):weipai2(name,1){}
weipai2(int sex):weipai2("ch",sex){}
weipai2():weipai2(1){}
};
class biaozhun
{
private:
template<class T> biaozhun(T first,T last):l(first,last){}
list<int> l;
public:
biaozhun(vector<short> &v):biaozhun(v.begin(),v.end()){};
biaozhun(deque<short> &d):biaozhun(d.begin(),d.end()){};
};
int main()
{
son1 m_son(1);
//son1 m_son2 ;编译失败
m_son.set_value(4);
cout<<m_son.get()<<endl;
vector<int> m_vector(3,2); //创建一个大小为3,所有元素只为2的vector容器
for(auto x = m_vector.begin();x != m_vector.end();x++)
{
cout<<*x<<endl;
}
cout<<"========================================"<<endl;
vector<short> v;
biaozhun m_biaozhun(v);
// cout<<v.begin()<<" "<<v.end()<<endl;
for(auto x = v.begin();x != v.end();x++)
{
cout<<*x<<endl;
}
return 0;
}
</span>