Base:4.5 和 Base:4struct Base {void f(double i){ cout << "Base:" << i << endl; }
};
struct Derived : Base {
};Derived d;
d.f(4.5); // Base:4.5
d.f(4); // 做了隐式转换 Base:4
如果使用子类使用 using Base::f;方式声明,指定继承基类的哪个方法,继承基类的成员函数的话,如下:
struct Base {void f(double i){ cout << "Base:" << i << endl; }
};
struct Derived : Base {using Base::f;void f(int i) { cout << "Derived:" << i << endl; }
};Derived d;
d.f(4.5); // Base:4.5
d.f(4); // Derived:4
class Info {
public:Info() { InitRest(); } // 目标构造函数// 委派构造函数,只能函数体内给type赋初值,不能使用初始化列表Info(int i) : Info() { type = i; }// 委派构造函数,只能函数体内给name赋初值,不能使用初始化列表Info(char e): Info() { name = e; }
private:void InitRest() { /* 其他初始化 */ }int type {1};char name {'a'};// ...
};
初始化的方式,列表初始化可以在“{}”花括号之前使用等号,其效果与不带使用等号的初始化相同。
vector v1 = {1, 3, 3};vector v2{1, 3, 3};
构造函数参数依靠C++11提供的initializer_list类模板,即可以获得列表初始化时的参数列表。
class Test
{
public:Test(initializer_list parm){for (auto &e : parm) {m_list.push_back(e);}}void Print() const{for (auto &e : m_list) {cout< m_list;
};int main()
{Test t{"aaa", "bbb", "ccc"};t.Print();return 0;
}
enum Gender {boy, girl};
class People {
public:People(initializer_list> l) { // initializer_list的构造函数auto i = l.begin();for (;i != l.end(); ++i)data.push_back(*i);}
private:vector> data;
};int main()
{People ship2012 = {{"Garfield", boy}, {"HelloKitty", girl}};return 0;
}
最为直观的解决方式就是对返回类型进行类型推导。而最为直观的书写方式如下所示:
templatedecltype(t1 + t2) Sum(T1 & t1, T2 & t2) {return t1 + t2;}
这样的写法虽然看似不错,不过对编译器来说有些小问题。编译器在推导decltype(t1 + t2)时的,表达式中的t1和t2都未声明(虽然它们近在咫尺,编译器却只会从左往右地读入符号)。按照C/C++编译器的规则,变量使用前必须已经声明,因此,为了解决这个问题,C++11引入新语法—追踪返回类型,来声明和定义这样的函数
templateauto Sum(T1 & t1, T2 & t2) -> decltype(t1 + t2){return t1 + t2;}
复合符号-> decltype(t1 + t2)被称为追踪返回类型。而原本函数返回值的位置由auto关键字占据。这样,我们就可以让编译器来推导Sum函数模板的返回类型了。而auto占位符和->return_type也就是构成追踪返回类型函数的两个基本元素。
上一篇:容器运行时与k8s概述