首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Class declaration

类和结构是由类说明符定义的用户定义类型,这些类型出现在声明语法。类说明符具有以下语法:

class-key attr class-head-name base-clause { member-specification }

?

?

class-key

-

one of class or struct. The keywords are identical except for the default member access and the default base class access.

attr(C++11)

-

optional sequence of any number of attributes, may include alignas specifier

class-head-name

-

the name of the class that's being defined. Optionally qualified, optionally followed by keyword final. The name may be omitted, in which case the class is unnamed (note that unnamed class cannot be final)

base-clause

-

optional list of one or more parent classes and the model of inheritance used for each (see derived class)

member-specification

-

list of access specifiers, member object and member function declarations and definitions (see below)

见班有关语法的一般概述。如果类键是union,声明引入了联合型...

前向申报

以下形式的声明。

class-key attr identifier ;

?

?

声明一个类类型,该类类型将在此作用域的后面定义。在定义出现之前,这个类名具有不完全类型这允许相互引用的类:

二次

代码语言:javascript
复制
class Vector; // forward declaration
class Matrix {
    // ...
    friend Vector operator*(const Matrix&, const Vector&);
};
class Vector {
    // ...
    friend Vector operator*(const Matrix&, const Vector&);
};

二次

如果一个特定的源文件只使用指向类的指针和引用,那么就可以减少#include依赖:

二次

代码语言:javascript
复制
// in MyStruct.h 
#include <iosfwd> // contains forward declaration of std::ostream
struct MyStruct {
    int value;
    friend std::ostream& operator<<(std::ostream& os, const S& s);
    // definition provided in MyStruct.cpp file which uses #include <ostream>
};

二次

如果前向声明出现在本地范围内,则生皮以前声明的类、变量、函数和所有其他名称相同的声明可能出现在包围作用域中:

二次

代码语言:javascript
复制
struct s { int a; };
struct s; // does nothing (s already defined in this scope)
void g() {
    struct s; // forward declaration of a new, local struct "s"
              // this hides global struct s until the end of this block
    s* p;     // pointer to local struct s
    struct s { char* p; }; // definitions of the local struct s
}

二次

注意,新的类名也可能由精化类型说明符作为另一个声明的一部分,但只有在名称查找能%27T找到以前声明过的同名类。

二次

代码语言:javascript
复制
class U;
namespace ns{
    class Y f(class T p); // declares function ns::f and declares ns::T and ns::Y
    class U f(); // U refers to ::U
    Y* p; T* q; // can use pointers and references to T and Y
}

二次

成员规格

成员规范,或体体类的定义,是以下任意数量的大括号括起来的序列:

1%29份表格成员声明

attr(optional) decl-specifier-seq(optional) member-declarator-list(optional) ;

?

?

attr(C++11)

-

optional sequence of any number of attributes

decl-specifier-seq

-

sequence of specifiers. It is only optional in the declarations of constructors, destructors, and user-defined type conversion functions

member-declarator-list

-

similar to a init-declarator-list, but additionally allows bit-field declaration, pure-specifier, and virt-specifier (override or final) (since C++11), and does not allow direct-non-list-initialization syntax.

本声明可声明静态非静态数据成员和成员函数成员打字机成员枚举,和嵌套类也可能是朋友声明...

二次

代码语言:javascript
复制
class S {
    int d1; // non-static data member
    int a[10] = {1,2}; // non-static data member with initializer (C++11)
    static const int d2 = 1; // static data member with initializer
    virtual void f1(int) = 0; // pure virtual member function
    std::string d3, *d4, f2(int); // two data members and a member function
    enum {NORTH, SOUTH, EAST, WEST};
    struct NestedS {
        std::string s;
    } d5, *d6;
    typedef NestedS value_type, *pointer_type;
};

二次

2%29个函数定义,声明和定义成员函数或朋友函数成员函数定义后的分号是可选的。在类体中定义的所有函数都是自动的。内联...

二次

代码语言:javascript
复制
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()(size_t r, size_t c) const { // member function definition
        return data[r*C+c];
    }
    int& operator()(size_t r, size_t c) {  // another member function definition
        return data[r*C+c];
    }
};

二次

3%29访问说明符public:,,,protected:,和private:

二次

代码语言:javascript
复制
class S {
 public:
    S();          // public constructor
    S(const S&);  // public copy constructor
    virtual ~S(); // public virtual destructor
 private:
    int* ptr; // private data member
};

二次

4%29使用-声明

二次

代码语言:javascript
复制
class Base {
 protected:
     int d;
};
class Derived : public Base {
 public:
    using Base::d; // make Base's protected member d a public member of Derived
    using Base::Base; // inherit all parent's constructors (C++11)
};

二次

5%29静态[医]断言声明

二次

代码语言:javascript
复制
template<typename T>
struct Foo {
  static_assert(std::is_floating_point<T>::value, "Foo<T>: T must be floating point");
};

二次

6%29成员模板声明

二次

代码语言:javascript
复制
struct S {
    template<typename T>
    void f(T&& n);
 
    template<class CharT>
    struct NestedS {
        std::basic_string<CharT> s;
    };
};

二次

7%29别名声明

二次

代码语言:javascript
复制
template <typename T>
struct identity
{
    using type = T;
};

二次

局部类

类声明可以出现在命名空间范围%28中--在这种情况下,它定义了一个普通类%29,在另一个类定义中,它定义了嵌套类%29,在函数的主体内,在这种情况下,它定义了局部类此类类的名称仅存在于函数范围内,不能在外部访问。

  • 本地类不能有静态成员。
  • 本地类的成员函数没有链接。
  • 本地类的成员函数必须完全在类体内定义。
  • 本地类闭包类型%28因为C++14%29不能有成员模板
  • 本地类不能有朋友模板
  • 本地类无法定义朋友函数在类定义中
  • 函数%28中的本地类(包括成员函数%29)可以访问与封闭函数可以访问的名称相同的名称。

local classes could not be used as template arguments

(until C++11)

  • 本地类不能用作模板参数

%28直到C++11%29

二次

代码语言:javascript
复制
#include <vector>
#include <algorithm>
#include <iostream>
 
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()); // since C++11
    for(int n: v) std::cout << n << ' ';
}

二次

产出:

二次

代码语言:javascript
复制
3 2 1

二次

另见

C结构声明文件

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com