【c++】类中带默认参数的函数

简介:

反思两个问题

  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

 



本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/3495920.html,如需转载请自行联系原作者

相关文章
|
1天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
1天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
2天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
2天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
17 0
|
5天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
7天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
27天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
34 0
|
27天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
32 0
|
7天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”

热门文章

最新文章