C++ 学习笔记(一些新特性总结3)
public、protected 和 private 继承
public 继承时,基类的存取限制是不变的。
class MyClass
{
public: // Unrestricted access
int myPublic;
protected: // Defining or derived class only
int myProtected;
private: // Defining class only
int myPrivate;
}
class MyChild1 : public MyClass
{
// myPublic is public
// myProtected is protected
// myPrivate is private
};
protected 继承时,基类的 public 成员变成 protected 成员。
class MyChild2 : protected MyClass
{
// myPublic is protected
// myProtected is protected
// myPrivate is private
};
private 继承时。基类的全部成员都变成 private 成员。这也是默认的继承方式。
class MyChild3 : private MyClass
{
// myPublic is private
// myProtected is private
// myPrivate is private
};
enum 类型的一些注意事项
同一个作用域内的 enum 名字非常easy冲突。
所以声明 enum 时要小心。
比方:
enum Color1 { Red = 3, Green = 4, Blue = 5 };
enum Color2 { Red = 30, Green = 40, Blue = 50 };
尽管是两个不同的 enum,可是 Color1 与 Color2 的 Red 、Green 和 Blue 会相互冲突。 所以应该尽量将 enum 限制在较小的范围内。
比方限制在一个 namespace 中或者限制在一个类中,当然也能够限制在一个函数中,只是这样就无法在函数外面訪问了。
class MyClass
{
enum Color { Red, Green, Blue };
};
void myFunction()
{
enum Color { Red, Green, Blue };
}
C++11 中引入了一种更安全的 enum。能够解决 enum 名字easy冲突的问题。以下是个样例。
enum class Color1 { Red = 3, Green = 4, Blue = 5 };
enum class Color2 { Red = 33, Green = 44, Blue = 55 };
int main()
{
Color2 c = Color2::Red;
cout << (int)c << endl;
}
可是这样的 enum 有点严格的过头了。连 enum 到 int 的默认类型转换都禁止了。以下的代码就会报错。
Color2 c = Color2::Red;
cout << c << endl; // error
if(c == 3) // error
这样的 enum 另一个特点。就是我们能够决定这个类型详细用什么类型的整型变量来实现。比方以下的样例:
enum class Color1 : long { Red = 3, Green = 4, Blue = 5 };
enum class Color2 : char { Red = 33, Green = 44, Blue = 55 };
int main()
{
cout << sizeof(Color1) << endl;
cout << sizeof(Color2) << endl;
}
constexpr keyword
C++14 中进一步扩展了常量这个概念。一个表达式假设能够在编译时就确定它的值,那么它就能作为一个常量来使用。比方以下这个样例用一个函数的返回值来作为一个数组的长度。
constexpr int getDefaultSize(int multiplier)
{
return 3 * multiplier;
}
// Compile-time evaluation
int myArray[getDefaultSize(10)];
[[deprecated]]
C++14 中引入了 [[deprecated]] 这个属性,能够用来修饰一个函数或对象。比方:
// Mark as deprecated
[[deprecated]] void foo() {}
编译代码时, foo() 这个函数被调用编译器会给出警告。
noexcept keyword
用来指示一个函数不会抛出不论什么异常。
有助于编译器去优化代码。
void foo() noexcept {} // may not throw exceptions
变量模板
在 C++14 之前, 仅仅有函数模板和类模板。C++14 又引入了一种新的模板类型,称为变量模板。以下是个样例:
template<class T>
constexpr T pi = T(3.1415926535897932384626433L);
这样 pi 就成了一个变量模板,用它能够给其它变量初始化。
int i = pi<int>; // 3
float f = pi<float>; // 3.14...