赞
踩
目录
2. 不能从外部访问类成员,公有成员函数如此,要调用公有成员函数,必须通过对象
4. 使用成员运算符(.)、简介成员运算符(->)或作用解析运算符(::)
C++11提供一种新的枚举, class 或者 struct
在类定义中的 名称作用域都为整个类,作用域为 整个类值在该类是已知的
- class Stock {
- public:
- int memi;
- double memd;
- };
-
- class Jodbj {
- public:
- int memi;
- double memd;
- };
Stock sleeper(........);
sleeper.show();
show();//这是错的!
- void Stock::update(double price)
- {
- ...
- }
- Class obj; // Class is some class type
- Class *ptr = &obj;
-
- // member is a data member of that class
- ptr->member; // fetches member from the object to which ptr points
- obj.member; // fetches member from the object named obj
-
- // memfcn is a function member of that class
- ptr->memfcn(); // runs memfcn on the object to which ptr points
- obj.memfcn(); // runs memfcn on the object named obj
这样是错误的:
- class Bakery
- {
- private:
- const int Months = 12;
- double const[Months];
-
- }
声明类只是描述了对象的形式,并没有创建对象
在创建对象前,将没有用于存储值得空间。
- class Bakery
- {
- private:
- enum { Months =12 };
- double const[Months];
- }
这种声明枚举并不会创建类数据成员,也就是所有对象中都不包含枚举。
Months只是一个符号名称,在作用域遇到时自动替换。
- calss Bakery
- {
- private:
- static const int Months =12;
- double costs[Months];
- }
这将创建一个名为Months的常量,该常量将于其他静态变量存储在一起,而不是存储在对象中。
两个枚举定义中的枚举量可能会发生冲突
enum egg {big, small, large};
enum shirt {big, small, large};
上面无法编译,因为在相同作用域,发生问题
enum class egg{big, small, large};
enum class shirt{big, small, large};
如何使用?
egg choice = egg::large;
shirt FLUO= shirt::large;
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。