特2:全局 const 变量的作用域仍然是当前文件

文章目录
前言
提示:以下是本篇文章正文内容,下面案例可供参考
特1:C++中的 const 分为编译时常量与运行时常量
>.初始式是常量表达式的 const 对象称为 编译时常量 否则称为 运行时常量
>.编译时常量编译阶段就执行值替换了 值替换的过程类似宏展开
特2:全局 const 变量的作用域仍然是当前文件
>.这和添加了关键字的效果类似
一、默认参数与无名形参二、函数的重载三、构造函数初始化列表四、类的成员和const成员
成员函数
const 成员变量
const 成员函数
自身类型的静态成员
#include using namespace std;class B{public:const static B static_self;double length {1.0};double width {1.0};double height {1.0};B(double a,double b,double c):length{a},height{c} { };B(double side):B(side,side,side) {};//委托构造函数static void show(){};};const B B::static_self(2.2); //必须在类外定义 类内声明int main() {cout<
也可以将它定义成非const 类型的这样就可以修改了
五、const 对象六、类的作用域七、 关键字类八、封闭类和继承中各构造函数和析构函数执行先后单继承情况下
>.派生类必须要在其构造函数初始化列表指明其成员对象和基类如何初始化(有默认构造能力的成员对象和基类构造函数可以不用指明)多继承情况下虚继承情况下九、继承与派生
>.继承与派生是一个概念,只不过站的角度不同
虚继承:
向上转型(将派生类赋值给基类):
十、多态 ()虚析构函数:虚函数表:十二、抽像类与纯虚函数抽像类:十三、引用十四、RTTI (Run-Time Type )十五、运算符重载( )十六、模板
类模板:
显示专门化( )
非类型参数:
模板特性 : \bf{\color{red}{模板特性:}} 模板特性:
显示实例化:(多文件编程)
>.显示实例化后 就可以把模板的声明和定义分开到不同的文件中了
类模板和模板类的概念
模板的继承与派生
模板参数的作用域:
#include #include using namespace std;template class Pair{private:T1 key;//关键字T2 value;//值public:Pair(T1 k, T2 v) : key(k), value(v) { };bool operator < (const Pair & p) const;//如果用T1 T2 那将报错,declaration of template parameter 'T1' shadows template parametertemplate //函数模板作友元friend ostream & operator << (ostream & o, const Pair & p);};template bool Pair ::operator< (const Pair & p) const{//“小”的意思就是关键字小return key < p.key;}template //这就是模板的作用域ostream & operator << (ostream & o, const Pair & p){o << "(" << p.key << "," << p.value << ")";return o;}int main(){Pair, int> student("Tom", 29);Pair obj(12, 3.14);cout << student << " " << obj;return 0;}
类模板与友元
#include using namespace std;templateclass A{public:void Func(const T & p){cout << p.v;}};template class B{private:T v;public:B(T n) : v(n) { }template friend class A;//把类模板A声明为友元};int main(){B b(5);A< B > a;//用B替换A模板中的 Ta.Func(b);return 0;}
【特2:全局 const 变量的作用域仍然是当前文件】类模板中的静态成员
#include using namespace std;template class A{private:static int count;public:A() { count ++; }~A() { count -- ; };A(A &) { count ++ ; }static void PrintCount() { cout << count << endl; }};template<> int A::count = 0;template<> int A::count = 0;//不同的模板类中的静态成员是不同的内存int main(){A ia;A da;//A f; error: undefined reference to `A::count'ia.PrintCount();da.PrintCount();return 0;}
十七、如此,func() 函数就不能抛出任何类型的异常了 , 即使抛出了,try 也检测不到.多态时派生类异常规范只能比基类限制更多(少一个参数),后来的 C++11 已经将它抛弃了,不再建议使用 。十八、copy构造函数十九、四种类型转换关键字二十、函数对象二十一、二十二、内联函数二十三、函数的可变参数To be …总结
late than never