7.6
来源:互联网 发布:c语言三元表达式 编辑:程序博客网 时间:2024/05/29 17:46
7.56
静态成员是与类相关,而不是与对象相关
所以此类的对象共用一个静态成员
当所有对象的一个成员是一样的时候,我们没必要为没一个对象产生一个这个成员,定义一个静态成员就行了
静态成员可以是不完全类型
指针成员也可以是不完全类型
普通成员必须是完全类型
静态成员可以作为默认实参
7.57
#include <string>class Account {public: void calculate() { amount += amount * interestRate; } static double rate() { return interestRate; } static void rate(double newRate) { interestRate = newRate; }private: std::string owner; double amount; static double interestRate; static constexpr double todayRate = 12.34; static double initRate() { return todayRate; }};double Account::interestRate = initRate();
7.58
//example.h#include<iostream>#include<vector>using namespace std;class Example{public: static double rate = 6.5; //不正确 static const int vecSize = 20;//正确 static vector<double> vec(vecSize);//vector是模板不是字面值常量类型};//example.cpp#include "example.h"double Example::rate;vector<double> Example::vec;//只能进行声明,不能定义 定义要在cpp中
改正
// example.hclass Example {public: static constexpr double rate = 6.5; static const int vecSize = 20; static vector<double> vec;};// example.C#include "example.h"constexpr double Example::rate;vector<double> Example::vec(Example::vecSize);
0 0
- 7.6
- 7.6
- 7.6
- 7.6
- 7.6
- 7.6
- 7.6
- 7.6
- 7.6随笔
- 7.6 泛型
- 7.6 7.7
- 7.6 调试
- 习题7.6
- 习题7.6
- sumary ~~~7.2-7.6
- 第七章 问题7.6
- 7.6. Protecting a Host
- 期待Mesa-7.6.1
- Atitit gui界面ui技术发展史与未来趋势
- 第一次写博客
- 使用数据库实现web留言本
- hive大数据倾斜总结
- 散列入门(1)----PAT-b1029. 旧键盘(20)
- 7.6
- Atitit nodejs5 nodejs6 nodejs 7.2.1 新特性attialx总结
- 463_GreenDao框架使用
- 基于Ada语言的RTEMS应用开发
- HDU 2602 Bone Collector (0-1背包)
- 464_HttpURLConnection的post
- 465_画出部分透明的蒙版
- 数学知识
- How to Extend/Reduce LVM’s (Logical Volume Management) in Linux