第二章:保证稳定性和兼容性

2.7 快速初始化成员变量

  • C++98:
    • 使用 ‘=’ 初始化类中成员变量,成员变量必须满足:
      ① static ② const ③ 整型或枚举型
class Init {
public:
    Init() : a(0) {}
    Init(int d) : a(d) {}

private:
    int a;
    const static int b = 0;  // ok
    int c = 1;               // error
    static int d = 0;        // error
    static const double e = 1.3;      // error,不是整型或枚举型
    static const char *const f = "e"; // error, 不是整型或枚举型
}
  • C++11
    • 允许非静态成员变量的初始化,且有多种形式。
struct {
    int a = 1;         // 使用 '=' 初始化
    double e {2.3};    // 使用 '{}' 初始化
};
#include <string>

using namespace std;

struct C {
    C(int i) :
        c(i) {}

    int c;
};

struct Init {
    int a = 1;
    string b("Hello");  // error
    C c(1);             // error
}

圆括号表达式初始化非静态成员 b 和 c 都会出错。

  • C++11 支持就地初始化非静态成员的同时,又支持初始化列表。如果两者同时使用,是否会冲突?
#include <iostream>
using namespace std;

struct Mem {
    Mem() { cout << "Mem defulat, num = " << num << endl; }
    Mem(int i) 
        : num(i) {
            cout << "Mem defulat, num = " << num << endl;
        }

    int num = 2; // 使用 = 初始化非静态成员
}

class Group {
public:
    Group() { cout << "Group default. val: " << val << endl; }
    Group(int i)
        : val('G'),
          a(i) {
              cout <<"Group. val: " << val << endl;
          }
    void NumofA() { cout << "number of A: " << a.num << endl; }
    void NumofB() { cout << "number of B: " << b.num << endl; }

private:
    char val{'g'}; // 使用 {} 初始化非静态成员
    Mem a;
    Mem b{19};     // 使用 {} 初始化非静态成员
}

int main() {
    Mem member;  // Mem defulat, num = 2
    Group group; // Mem default, num = 2
                 // Mem default, num = 19
                 // Group default. val: g

    group.NumOfA();  // number of A: 2
    group.NumOfB();  // number of B: 19

    Group group2(7); // Mem defulat, num = 7
                     // Mem defulat, num = 19
                     // Group. val: G

    group2.NumOfA();  // number of A: 7
    group2.numOfB();  // number of B: 19
}

2.8 非静态成员的 sizeof

  • C++98
    • 无法对非静态成员变量使用sizeof
#include <iostream>
using namespace std;

struct People {
public:
    int hand;
    static People *all;
}

int main()
{
    Pople p;
    cout << sizeof(p.hand) << endl;       // C++98 ok, C++11 ok
    cout << sizeof(Pople::all) << endl;   // C++98 ok, C++11 ok
    cout << sizeof(People::hand) << endl; // C++98 err, C++11 ok
}

2.9 扩展的 friend 语法

friend 关键字用于声明类的 友元, 友元可以无视类中的成员属性。无论是public、protected或private,友元类或友元函数都可以访问,这完全破坏了面向对象中封装性的概念。通常,转件建议使用 Get/Set 方法访问类成员,但是,friend会使程序员少些很多代码。

class Poly;
typedef Poly P;

class LiLei {
    friend class Poly; // C++98 ok, C++11 ok
};
class Jim {
    friedn Poly;       // C++98 error, C++11 ok
};
class HanMeiMei {
    friend P;          // C++98 error, C++11 ok
}

程序员可以为类模板声明友元

class P;
template <typename T>
class People {
    friend T;
};

People<P> pp;   // 类型 P 在这里是 People 类型的友元
People<int> Pi; // 对于 int 类型模板参数,友元声明被忽略
// 为了方便测试,进行了危险的定义
#ifdef UNIT_TEST
#define private public
#endif
class Defender {
public:
    void Defence(int x, int y) {}
    void Trackle(int x, int y) {}

private:
    int pos_x = 15;
    int pos_y = 0;
    int speed = 2;
    int stamina = 120;
};

class Attacker {
public:
    void Move(int x, int y) {}
    void SpeedUp(float ration) {}

private:
    int pos_x = 0;
    int pos_y = -30;
    int speed = 3;
    int stamina = 100;
};

#ifdef UNIT_TEST
class Validator {
public:
    void Validate(int x, int y, Defender & d) { }
    void Validate(int x, int y, Attacker & a) { }
};

int main() {
    Defender d;
    Attacker a;
    a.Move(15, 30);
    d.Defence(15, 30);
    a.SpeedUp(1.5f);
    d.Defence(15, 30);
    Validator v;
    v.Validate(7, 0, d);
    v.Validate(1, -10, a);
    return 0;
}
#endif

将 private 关键字统一替换成了 public 关键字。

template <typename T>
class DefenderT {
public:
    friend T;
    void Defence(int x, int y) {}
    void Trackle(int x, int y) {}

private:
    int pos_x = 15;
    int pos_y = 0;
    int speed = 2;
    int stamina = 120;
};

template <typename T>
class AttackerT {
public:
    friend T;
    void Move(int x, int y) {}
    void SpeedUp(float ration) {}

private:
    int pos_x = 0;
    int pos_y = -30;
    int speed = 3;
    int stamina = 100;
};

using Defender = DefenderT<int>;
using Attacker = AttackerT<int>;

class Validator {
public:
    void Validate(int x, int y, Defender & d) { }
    void Validate(int x, int y, Attacker & a) { }
};

using DefenderTest = DefenderT<Validator>;
using AttackerTest = AttackerT<Validator>;

int main() {
    Defender d;
    Attacker a;
    a.Move(15, 30);
    d.Defence(15, 30);
    a.SpeedUp(1.5f);
    d.Defence(15, 30);
    Validator v;
    v.Validate(7, 0, d);
    v.Validate(1, -10, a);
    return 0;
}

2.10 final/override 控制

  • 重载(overload):是指同一可访问区内被声明的几个具有不同参数列(参数的类型,个数,顺序不同)的同名函数,根据参数列表确定调用哪个函数,重载不关心函数返回类型。
  • 重写(override):派生类中存在重新定义的函数。其函数名,参数列表,返回值类型,所有都必须同基类中被重写的函数一致。只有函数体不同(花括号内),派生类调用时会调用派生类的重写函数,不会调用被重写函数。重写的基类中被重写的函数必须有virtual修饰。
#include <iostream>
using namespace std;

class MathObject {
public:
    virtual double Arith() = 0;
    virtual void Prin() = 0;
};

class Printable : public MathObject {
public:
    double Arith() = 0;
    void Print() {  // C++98中我们无法阻止该接口被重写
        cout << "Output is : " << Arith() << endl;
    }
};

class Add2 : public Printable {
public:
    Add2(double a, double b)
    : x(a),
      y(b) { }

    double Arith() {
        return x + y;
    }

private:
    double x, y;
};

class Mul3 : public Printable {
public:
    Mul3(double a, double b, double c)
    : x (a),
      y (b),
      z (c) {}

    double Arith() {
        return x * y * z;
    }
private:
    double x, y, z;
};

和 Java 类似,通过 final 关键字阻止 函数继续重写。

struct Object {
    virtual void fun() = 0;
};
struct Base : public Object {
    void fun() final; // 声明为 final
};
struct Derived : public Base {
    void fun();       // 无法通过编译
};

final 同样可以终止虚函数被重写,但这没有意义。

为了便于阅读,发现类中的重写方法,引入 override 关键字。

如果派生类在虚函数声明时使用了 override 描述符,那么该函数必须重载其基类中的同名函数,否则无法编译。

struct Base {
    virtual void Turing() = 0;
    virtual void Dijkstra() = 0;
    virtual void VNeumann(int g) = 0;
    virtual void DKnuth() const;
    void Print();
};

struct DerivedMid: public Base {
    // void VNeumann(double g);
    // 接口被隔离了,曾想多一个版本的 VNeumann 函数
};

struct DerivedTop: public DerivedMid {
    void Turing() override;
    void Dikjstra() override;        // error,拼写错误
    void VNeumann(char g) override;  // error,参数不一致
    void DKnuth() override;          // error, const 属性不一致
    void Print() override;           // error,非虚函数
};

2.11 模板函数的默认模板参数

#include <iostream>
using namespace std;

// 定义一个函数模板
template<typename T>
void TemFun(T a) {
    cout << a << endl;
}

int main() {
    TempFun(1);    // TempFun<const int>(1)
    TempFun("1");  // TempFun<const char *>("1")
}
  • 默认模板参数
template<typename T1, typename T2 = int>
class DefClass1;
template<typename T1 = int, typename T2>
class DefClass2;   // error

template<typename T, int i = 0> 
class DefClass3;
template<int i = 0, typename T>
class DefClass4; // error

template<typename T1 = int, typename T2>
void DefFunc1(T1 a, T2 b);
template<int i = 0, typename T>
void DefFunc2(T a);
  • 推导规则:如果能从函数实参中推导出类型的话,默认模板参数就不会使用
template<class T, class U = double>
void f(T t = 0, U u = 0);

void g() 
{
    f(1, 'c');  // f<int, char>(1, 'c')
    f(1);       // f<int, double>(1, 0)
    f();        // error, T 无法推导
    f<int>();   // f<int, double>(0, 0)
    f<int, char>(); // f<int, char>(0, 0)
}

2.12 外部模板

2.12.1 为什么需要外部模板?

C 中 extern 的目的:

extern int i;

一个文件定义 i, 多个文件声明 i, 但是 i 只有一份数据。

对函数模板来说,存在一模一样的问题。不同的是,发生问题的不是变量,而是函数。

// test.cpp
template<typename T>
void fun(T) {
}

// test1.cpp
#include "test.h"
void test1() { fun(3); }

// test2.cpp
#include "test.h"
void test2() { fun(4); }
/*
 * 问题:
 * 由于两个源代码使用的模板函数的参数类型一致,所以再编译 test1.cpp 时编译器会实例化 fun<int>(int).
 * 在编译 test2.cpp 时,编译器会再一次实例化函数 fun<int>(int)。
 * 那么结果就是 test1.o 和 test2.o 会有两份一模一样的函数 fun<int>(int) 代码。 
*/

代码重复,为了节省空间,保留其中之一就可以了。事实上,大部分链接器也是这么做的。链接器通过一些编译器辅助的手段将重复的模板函数代码 fun<int>(int) 删除掉,只保留了单个副本。

问题是:对于源码中的每一处模板实例化,编译器都需要去做实例化的工作;而在链接时,链接器还需要删除重复的实例化代码。很明显,这太麻烦。

2.12.2 显式的实例化与外部模板的声明

// 显式实例化
template <typename T> 
void fun(T) {

}

template <typename int>(int);

编译器编译时会强制实例化 fun<int>(int) 函数。

// 外部模板
extern template void fun<int>(int);
// test1.cpp
template void fun<int>(int); // 显式实例化
void test1() {
    fun(3);
}
// test2.cpp
extern template void fun<int>(int);  // 外部模板声明
void test() {
    fun(3);
}

注意问题:如果外部模板声明出现在某个编译单元中,那么与之对应的显式实例化必须出现于另一个编译单元中或同一个编译单元的后续代码中。

外部模板声明不能用于一个静态函数(文件域函数),但是可以用于类静态成员函数(因为静态函数没有外部链接属性,不能再本编译单元外出现)。

2.13 局部和匿名类型作模板实参

  • C++98:
    • 局部类型和匿名类型在C++98中不能做模板的实参
template <typename T>
class X {};

template <typename T>
void TempFun(T t) {};

struct A{} a;
struct {int i;} b;          // b 是匿名类型变量
typedef struct {
    int i;
} B;                        // B 是匿名类型

void Fun()
{
    struct C{} c;         // C 是局部类型

    X<A> x1;           // C++98 error, C++11 ok
    X<B> x2;           // C++98 error, C++11 ok
    X<C> x3;           // C++98 error, C++11 ok
    TempFun(a);        // C++98 error, C++11 ok
    TempFun(b);        // C++98 error, C++11 ok
    TempFun(c);        // C++98 error, C++11 ok
}
template <typename T>
struct MyTemplate {};

int main()
{
    MyTemplate<struct {int a;}> t; // error
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,100评论 1 32
  • 婚姻杀手 瘦子:你有过离婚的想法吗 胖子:当然有,刚生完老大那时候 瘦子:我也是,生完孩子,家里全乱套了 胖子:产...
    冉曦淅阅读 246评论 0 0
  • 一阵摇曳的风 拽扯着你柔韧的茎 你用执著牢牢扣住 躯干的营养 撇不下妈妈的怀抱 可一夜间 你从翠绿到金黄 依然抵挡...
    南山台子阅读 288评论 1 4
  • CFRunLoopModeRunLoop在同一时段只能且必须在一种特定Mode下Run更换Mode时, 需要暂停当...
    胡图仙人阅读 160评论 0 0
  • 上午涝池评审会; 下午五点暴雨,正担心怎么回家呀,快六点雨住了,竟有点“东风知我欲山行,吹断檐间积雨声”的意思; ...
    七月紫苏阅读 495评论 0 0