【c++】类中带默认参数的函数
C++ 函数 参数 默认
2023-09-27 14:25:21 时间
反思两个问题
1. 带默认参数的函数,为何声明、定义不能同时有参数?
2. 带默认参数的函数, 为何带默认参数的参数靠后站?
上程序
#include <iostream> #include <string> using namespace std; class A { public: A(const string &a = "hello, nihao!", int b = 67); private: string s; int sb; }; A::A(const string &a, int b) : s(a), sb(b) { cout << s << " " << sb << endl; } int main() { string s = "hello"; A a("hello" ,23); }
类A的构造函数,参数带着默认值,这样,建立类的对象时可以不给类对象赋全部的参数(没有的参数会用默认值顶上),这里有几个个问题:
问题1: 带参数的构造函数如果声明时带上默认的参数,那么在定义时就不能带了。
这样设计的目的,我认为是这样:如果声明给定了一个参数了,但是定义时又给定了一个不同的参数。这样,用户看到的是类的声明.h文件,但两者不一样,会按着定义来。这就给类的使用者带来了麻烦,因此,为了避免这样,带默认参数的函数,两者只能在一个地方出现,不能同时出现。
其实,不止是构造函数中出现这样的问题,所有的函数,都应该这样:声明和定义的默认默认参数不共戴天----不能同时都有(但是,建议要是有的话,最好在声明时写上)。例如:
#include <iostream> #include <string> #include <set> #include <map> using namespace std; class A { public: A(const string &a = "hello, nihao!", int b = 67); void print_A(int a = 34); private: string s; int sb; }; A::A(const string &a = "hello, nihao", int b = 67) : s(a), sb(b) { cout << s << " " << sb << endl; } void A::print_A(int a) { cout << a << endl; } int main() { string s = "hello"; A a("hello" ,23); }
错误提示
del7.cc:17:50: error: default argument given for parameter 1 of ‘A::A(const string&, int)’ del7.cc:10:3: error: after previous specification in ‘A::A(const string&, int)’ del7.cc:17:50: error: default argument given for parameter 2 of ‘A::A(const string&, int)’ [-fpermissive] del7.cc:10:3: error: after previous specification in ‘A::A(const string&, int)’ [-fpermissive]
把定义中默认的参数去掉就行了。
问题2. 带默认参数的函数, 为何带默认参数的参数靠后站?
我这么理解:从前往后写,可以少写,定义对象时,不够的用默认的参数补上,挺好。反之,如果带参数的往前写那么当定义对象时,前边的有参数,又给传了实参;而后边的没有参数,也不传,这不浪费吗。
例证
#include <iostream> #include <string> #include <map> using namespace std; class B { public: B(int c = 5, const string &s = "newfldr") { mapa.insert(make_pair<B*, int>(this, c)); } void print_Bsize() { cout << mapa.size() << endl; } private: static map<B*, int> mapa; }; map<B*, int> B::mapa; //静态变量,单独在类外开个小灶 int main() { B b(2, "ni"); B b1(3); //参数预定义不同,因为有默认的参数,没问题儿 B b2(4, "ni2"); b.print_Bsize(); //因为mapa是类的,所以这里不论是谁,结果都是一样的 }
结果
3
相关文章
- 【c++STL——第七讲】priority_queue系列 (常用知识点总结)
- C++之构造函数、参数列表、析构函数
- C/C++unlink函数的使用
- Linux chroot命令函数介绍(C/C++)
- C++ virtual函数重写,在继承的时候没有在函数前写virtual关键字也依然是虚函数吗?
- c++ template函数的声明和实现需要在同一个文件中
- 【C++】基础知识
- 使用OpenCV的函数createTrackbar()创建窗口滑动条查找图像二值化的最优阈值的C++源码
- 在OpenCV1.0环境下写的图像平移函数(C/C++代码)
- C++中可以以“类名::成员函数”直接引用成员函数哦!
- 27 C++ - 对象成员变量和函数的存储
- C++第12周项目2——分段函数
- C++函数声明和定义
- C++ 纯虚函数和抽象类
- 《C和C++程序员面试秘笈》——1.8 C++与C有什么不同
- 2022年蓝桥杯:第十三届蓝桥杯大赛软件赛省赛C/C++大学B组真题(考后回顾,文末附真题链接)
- 《c++语言导学》——1.4 函数
- C/C++ 指针的非空判断
- 19、【顺序表】删除顺序表内所有指定元素(C++版)
- 编程参考 - C++的析构函数的个数只能有一个
- C++ 正则表达式