您的位置:首页 > 编程语言 > C语言/C++

C++默认参数的函数的记录

2016-12-01 20:26 176 查看
转自:https://www.cnblogs.com/kaituorensheng/archive/2013/12/28/3495920.html

反思两个问题

  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++