C/Cpp / 构造函数种类

本文详细解析了C++中的五种构造函数:默认构造函数、一般构造函数、复制构造函数、转换构造函数以及等号运算符重载构造函数。通过实例代码展示了每种构造函数的使用场景和特点。
2018-03-15  创建人:Ruo_Xiao
开发环境:VS2010
邮箱:xclsoftware@163.com

2018-04-16  修改人:Ruo_Xiao
添加对类型转换构造函数隐式转换的抑制使用的说明,即:explicit。

2019-06-19  修改人:Ruo_Xiao
修正对转换构造函数的说明。

一、默认构造函数(无参数构造函数)

Father()
{
    std::cout<<"我是默认构造函数!"<<std::endl;
    std::cout<<std::endl;
}

2、一般构造函数(重载构造函数)

  • 这种构造函数类中可以有多个,但是形参类型或者个数不能相同,需要满足C++重载原理。
  • 栗子:
Father(int i,double d)
{
    cout<<"我是一般构造函数!"<<endl;
    cout<<endl;
}

3、复制构造函数,即:拷贝构造函数

  • 拷贝构造函数参数为类对象本身的引用,用于根据一个已存在的对象复制出一个新的该类的对象。
  • 若没有显示的写拷贝构造函数,则系统会在适当的时候创建一个默认的拷贝构造函数
  • 栗子:
Father(const Father &F)
{
    cout<<"我是复制构造函数!"<<endl;
    cout<<endl;
}

4、转换构造函数

  • 条件:构造函数只有一个参数,而且该参数不是本类的const引用。
  • 作用:相当于C语言隐式类型转换,相当于 类Test t = 100; // 就是t = Test(100);
  • 栗子:
Father(int i)
{
    cout<<"我是转换构造函数!"<<endl;
    cout<<endl;
}

 

  • 上述属于隐式转换,若不编写者不想让调用者执行该操作,即:必须显示调用。则在构造函数声明前面加上“explicit”即可。

5、等号运算符重载构造函数(赋值构造函数)

  • 作用:两个对象的数据成员进行赋值。
  • 条件:等号两边的对象必须已经创建。
  • 栗子:
Father &operator=( const Father &rhs )
{
    cout<<"我是等号运算符重载构造函数!"<<endl;
    return *this;
}

 

(SAW:Game Over!)
 

In file included from /data/k50048780/cpp_mutator/data_types/string_.cpp:5: /data/k50048780/cpp_mutator/data_types/string_.h:22:17: error: invalid covariant return type for ‘virtual std::string String::mutate()’ 22 | std::string mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/data_types/string_.h:8, from /data/k50048780/cpp_mutator/data_types/string_.cpp:5: /data/k50048780/cpp_mutator/data_types/StringBase.h:19:26: note: overridden function is ‘virtual std::vector<unsigned char> StringBase::mutate()’ 19 | std::vector<uint8_t> mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/string/StringFormatRandom.h:9, from /data/k50048780/cpp_mutator/mutators/string/StringFormatRandom.cpp:5: /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:22:17: error: invalid covariant return type for ‘virtual std::string String::mutate()’ 22 | std::string mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:8, from /data/k50048780/cpp_mutator/mutators/string/StringFormatRandom.h:9, from /data/k50048780/cpp_mutator/mutators/string/StringFormatRandom.cpp:5: /data/k50048780/cpp_mutator/mutators/string/../../data_types/StringBase.h:19:26: note: overridden function is ‘virtual std::vector<unsigned char> StringBase::mutate()’ 19 | std::vector<uint8_t> mutate() override; | ^~~~~~ /data/k50048780/cpp_mutator/data_types/string_.cpp: In constructor ‘String::String(MutatorConfig, const string&, const string&)’: /data/k50048780/cpp_mutator/data_types/string_.cpp:38:56: error: no match for ‘operator=’ (operand types are ‘std::vector<unsigned char>’ and ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’}) 38 | this->value = !value.empty() ? value : std::string(); | ^ In file included from /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/vector:72, from /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/random.h:34, from /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/random:49, from /data/k50048780/cpp_mutator/data_types/../mutators/config.h:7, from /data/k50048780/cpp_mutator/data_types/StringBase.h:9, from /data/k50048780/cpp_mutator/data_types/string_.h:8, from /data/k50048780/cpp_mutator/data_types/string_.cpp:5: /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/vector.tcc:198:5: note: candidate: ‘std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ 198 | vector<_Tp, _Alloc>:: | ^~~~~~~~~~~~~~~~~~~ /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/vector.tcc:199:42: note: no known conversion for argument 1 from ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’} to ‘const std::vector<unsigned char>&’ 199 | operator=(const vector<_Tp, _Alloc>& __x) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~ In file included from /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/vector:67, from /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/random.h:34, from /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/random:49, from /data/k50048780/cpp_mutator/data_types/../mutators/config.h:7, from /data/k50048780/cpp_mutator/data_types/StringBase.h:9, from /data/k50048780/cpp_mutator/data_types/string_.h:8, from /data/k50048780/cpp_mutator/data_types/string_.cpp:5: /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/stl_vector.h:706:7: note: candidate: ‘std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ 706 | operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move()) | ^~~~~~~~ /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/stl_vector.h:706:26: note: no known conversion for argument 1 from ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’} to ‘std::vector<unsigned char>&&’ 706 | operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move()) | ~~~~~~~~~^~~ /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/stl_vector.h:727:7: note: candidate: ‘std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ 727 | operator=(initializer_list<value_type> __l) | ^~~~~~~~ /data/taoli/c_compiler/gcc-9.5.0/include/c++/9.5.0/bits/stl_vector.h:727:46: note: no known conversion for argument 1 from ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’} to ‘std::initializer_list<unsigned char>’ 727 | operator=(initializer_list<value_type> __l) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~ /data/k50048780/cpp_mutator/data_types/string_.cpp: In member function ‘virtual std::string String::mutate()’: /data/k50048780/cpp_mutator/data_types/string_.cpp:64:12: error: could not convert ‘((String*)this)->String::<anonymous>.StringBase::<anonymous>.VirtualBase<std::vector<unsigned char> >::value’ from ‘std::vector<unsigned char>’ to ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} 64 | return value; | ^~~~~ | | | std::vector<unsigned char> In file included from /data/k50048780/cpp_mutator/mutators/string/StringCaseRandom.h:13, from /data/k50048780/cpp_mutator/mutators/string/StringCaseRandom.cpp:1: /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:22:17: error: invalid covariant return type for ‘virtual std::string String::mutate()’ 22 | std::string mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:8, from /data/k50048780/cpp_mutator/mutators/string/StringCaseRandom.h:13, from /data/k50048780/cpp_mutator/mutators/string/StringCaseRandom.cpp:1: /data/k50048780/cpp_mutator/mutators/string/../../data_types/StringBase.h:19:26: note: overridden function is ‘virtual std::vector<unsigned char> StringBase::mutate()’ 19 | std::vector<uint8_t> mutate() override; | ^~~~~~ CMakeFiles/mutator.dir/build.make:374: recipe for target 'CMakeFiles/mutator.dir/mutators/string/StringFormatRandom.cpp.o' failed make[3]: *** [CMakeFiles/mutator.dir/mutators/string/StringFormatRandom.cpp.o] Error 1 make[3]: *** Waiting for unfinished jobs.... In file included from /data/k50048780/cpp_mutator/mutators/string/StringLengthVariance.h:9, from /data/k50048780/cpp_mutator/mutators/string/StringLengthVariance.cpp:6: /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:22:17: error: invalid covariant return type for ‘virtual std::string String::mutate()’ 22 | std::string mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:8, from /data/k50048780/cpp_mutator/mutators/string/StringLengthVariance.h:9, from /data/k50048780/cpp_mutator/mutators/string/StringLengthVariance.cpp:6: /data/k50048780/cpp_mutator/mutators/string/../../data_types/StringBase.h:19:26: note: overridden function is ‘virtual std::vector<unsigned char> StringBase::mutate()’ 19 | std::vector<uint8_t> mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/string/StringCaseRandom.h:13, from /data/k50048780/cpp_mutator/mutators/MutatorFactory.cpp:7: /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:22:17: error: invalid covariant return type for ‘virtual std::string String::mutate()’ 22 | std::string mutate() override; | ^~~~~~ CMakeFiles/mutator.dir/build.make:134: recipe for target 'CMakeFiles/mutator.dir/data_types/string_.cpp.o' failed make[3]: *** [CMakeFiles/mutator.dir/data_types/string_.cpp.o] Error 1 In file included from /data/k50048780/cpp_mutator/mutators/MutatorFactory.h:11, from /data/k50048780/cpp_mutator/mutators/MutatorFactory.cpp:5: /data/k50048780/cpp_mutator/mutators/../data_types/StringBase.h:19:26: note: overridden function is ‘virtual std::vector<unsigned char> StringBase::mutate()’ 19 | std::vector<uint8_t> mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/string/StringCaseLower.cpp:8: /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:22:17: error: invalid covariant return type for ‘virtual std::string String::mutate()’ 22 | std::string mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/string/../../data_types/string_.h:8, from /data/k50048780/cpp_mutator/mutators/string/StringCaseLower.cpp:8: /data/k50048780/cpp_mutator/mutators/string/../../data_types/StringBase.h:19:26: note: overridden function is ‘virtual std::vector<unsigned char> StringBase::mutate()’ 19 | std::vector<uint8_t> mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/utils.cpp:5: /data/k50048780/cpp_mutator/mutators/../data_types/string_.h:22:17: error: invalid covariant return type for ‘virtual std::string String::mutate()’ 22 | std::string mutate() override; | ^~~~~~ In file included from /data/k50048780/cpp_mutator/mutators/../data_types/string_.h:8, from /data/k50048780/cpp_mutator/mutators/utils.cpp:5: /data/k50048780/cpp_mutator/mutators/../data_types/StringBase.h:19:26: note: overridden function is ‘virtual std::vector<unsigned char> StringBase::mutate()’ 19 | std::vector<uint8_t> mutate() override; | ^~~~~~
07-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值