Class declaration
类是由类说明符定义的用户定义类型,它出现在 声明 语法的 decl-specifier-seq 中。
内容语法类指定符具有以下语法:
1) 命名类定义
2) 未命名类定义
前置声明以下形式的声明
声明一个稍后将在当前作用域中定义的类类型。在定义出现之前,该类名具有 不完整类型 。这允许类之间相互引用: class Vector; // 前向声明 class Matrix { // ... friend Vector operator*(const Matrix&, const Vector&); }; class Vector { // ... friend Vector operator*(const Matrix&, const Vector&); }; 如果某个源文件仅使用类的指针和引用,这将有助于减少 #include 依赖: // 在 MyStruct.h 文件中 #include <iosfwd> // 包含 std::ostream 的前置声明 struct MyStruct { int value; friend std::ostream& operator<<(std::ostream& os, const S& s); // 定义在 MyStruct.cpp 文件中提供,该文件使用 #include <ostream> }; 如果在局部作用域中出现前向声明,它会 隐藏 先前声明的类、变量、函数以及可能出现在外围作用域中的所有同名声明: struct s { int a; }; struct s; // 无实际作用(s 已在当前作用域内定义) void g() { struct s; // 新局部结构体 s 的前向声明 // 这将隐藏全局结构体 s 直至当前块结束 s* p; // 指向局部结构体 s 的指针 struct s { char* p; }; // 局部结构体 s 的定义 } 请注意,新的类名也可能通过 详细类型说明符 引入,该说明符作为其他声明的一部分出现,但前提是 名称查找 无法找到先前声明的同名类。 class U; namespace ns { class Y f(class T p); // 声明函数 ns::f 并声明 ns::T 和 ns::Y class U f(); // U 指向 ::U // 可以使用指向 T 和 Y 的指针及引用 Y* p; T* q; } 成员声明成员规范,即类定义的 主体 ,是由花括号括起的任意数量以下元素的序列:
1)
形式如下的成员声明
此声明可以声明 静态 和非静态 数据成员 与 成员函数 、成员 类型别名 、成员 枚举 以及 嵌套类 。也可以是 友元声明 。 class S { int d1; // 非静态数据成员 int a[10] = {1, 2}; // 带初始化器的非静态数据成员 (C++11) static const int d2 = 1; // 带初始化器的静态数据成员 virtual void f1(int) = 0; // 纯虚成员函数 std::string d3, *d4, f2(int); // 两个数据成员和一个成员函数 enum { NORTH, SOUTH, EAST, WEST }; struct NestedS { std::string s; } d5, *d6; typedef NestedS value_type, *pointer_type; };
2)
函数定义,同时声明并定义
成员函数
或
友元函数
。成员函数定义后的分号是可选的。所有在类体内定义的函数都会自动成为
inline
,除非它们隶属于某个
命名模块
(since C++20)
。
class M { std::size_t C; std::vector<int> data; public: M(std::size_t R, std::size_t C) : C(C), data(R*C) {} // constructor definition int operator()(std::size_t r, std::size_t c) const // member function definition { return data[r * C + c]; } int& operator()(std::size_t r, std::size_t c) // another member function definition { return data[r * C + c]; } };
3)
访问说明符
public:
,
protected:
, 和
private:
class S { public: S(); // public constructor S(const S&); // public copy constructor virtual ~S(); // public virtual destructor private: int* ptr; // private data member };
4)
using声明
:
class Base { protected: int d; }; class Derived : public Base { public: using Base::d; // 将Base的受保护成员d设为Derived的公开成员 using Base::Base; // 继承所有基类的构造函数(C++11) };
5)
static_assert
声明:
template<typename T> struct Foo { static_assert(std::is_floating_point<T>::value, "Foo<T>: T must be floating point"); };
6)
成员模板声明
:
struct S { template<typename T> void f(T&& n); template<class CharT> struct NestedS { std::basic_string<CharT> s; }; };
局部类类声明可以出现在函数体内,这种情况下它定义的是 局部类 。此类名称仅存在于函数作用域内,在外部无法访问。
运行此代码
#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{1, 2, 3}; struct Local { bool operator()(int n, int m) { return n > m; } }; std::sort(v.begin(), v.end(), Local()); // 自 C++11 起 for (int n : v) std::cout << n << ' '; std::cout << '\n'; } 输出: 3 2 1 关键词缺陷报告以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
参见
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||