Class declaration

来自cppreference.com
< cpp‎ | language

 
 
C++语言

|- class="t-nv-h1" | colspan="5" | 一般主题 |- | |- |

预处理器
注释

|

|-

|- class="t-nv-h1" | colspan="5" | 流程控制 |-

条件执行语句

| |- |

|

|-

循环语句

| |- |

|

|-

跳转语句

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 函数 |-

函数声明 Lambda函数声明 inline说明符 异常说明符 (已弃用) noexcept说明符 (C++11)

|- class="t-nv-h1" | colspan="5" | Exceptions |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 命名空间 |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类型 |- | |- |

|

符合类型
联合类型

|-

说明符

| |- |

decltype (C++11)
auto (C++11)
alignas (C++11)

|

|-

存储持续期说明符 Initialization

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 表达式 |- | |- |

值类别
求值顺序

|

|-

替代表示 字面量 布尔型 - 整型 - 浮点型 字符型 - 字符串 - nullptr (C++11) 用户定义 (C++11)

|- class="t-nv-h1" | colspan="5" | Utilities |-

属性 (C++11) Types typedef声明 类型别名声明 (C++11) Casts 隐式类型转换 - 显示类型转换 static_cast - dynamic_cast const_cast - reinterpret_cast 内存分配

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类 |- | |- |

|

|-

类特有的函数属性

| |- |

|

explicit (C++11)
static

|-

静态成员函数

| |- |

|

|- |- class="t-nv-h1" | colspan="5" | 模板 |- | |- |

|

|- |- class="t-nv-h1" | colspan="5" | 杂项 |- | |- |

|

|-

 

目录

[编辑] 语法

class identifier { class_body } object_list ; (1)
class identifier : ancestor_list { class_body } object_list ; (2)
class identifier ; (3)
class identifier final opt_ancestors_and_body (4) (since C++11)

[编辑] Class Body

A list of member and friend declarations and access specifiers:

public: (1)
protected: (2)
private: (3)
friend friend_declaration (4)
member_declaration (5)
static member_declaration (6)
nested_type_declaration (7)

[编辑] Ancestor List

A list of classes that have already bee fully defined optionally prefixed with an access specifier

[编辑] Object List

An optional list of instances of the previously defined class

[编辑] 解释

  1. Defines a class and its member
  2. Defines a class inheriting other classes
  3. Forwards declares a class
  4. Defines a class that cannot be derived from ( see final )

If friend or member functions have their body defined inside the class body, they are implicitly inlined

[编辑] 注释

(since C++11) A default value can be assigned to data members inside the class body (ie: not necessarily in a constructor)

[编辑] 另请参阅


[编辑] 示例

class C;
 
class D : public B // B needs to be defined
{
  private:
    C *ptr_c; // a pointer/reference to C can be used as C has been forward declared
    double x = 12.3; // C++11 inline data member initialization
    static const int sci = 1; // this is valid in C++98 as well
  public:
    typedef B parent_type;
 
    // inline function
    virtual parent_type foo() const
    {
        return B();
    }
 
    // non-inline function declaration. needs to be defined externally
    void bar();
} D_obj; // An object of type D is defined
 
// definition of a class method outside the class
void D::bar()
{
   //...
}
来自“http://zh.cppreference.com/mwiki/index.php?title=cpp/language/class&oldid=38190