From cppreference.com
Classes are user-defined types, defined by class-specifier, which appears in decl-specifier-seq of the
syntax.
Syntax
The class specifier has the following syntax:
class-keyattr(optional)class-head-namefinal(optional)base-clause(optional){member-specification}(1) class-keyattr(optional)base-clause(optional){member-specification}(2) 1) Named class definition
2) Unnamed class definition
class-key- one of
,
and
. The keywords class and struct are identical except for the default
and the default
. If it is union, the declaration introduces a
. attr- (since C++11) any number of
, may include
specifier class-head-name- the name of the class that's being defined, optionally
final- (since C++11) if present, the class
base-clause- list of one or more base classes and the model of inheritance used for each (see
) member-specification- list of access specifiers, member object and member function declarations and definitions (
) Forward declaration
A declaration of the following form
class-keyattridentifier;Declares a class type which will be defined later in this scope. Until the definition appears, this class name has
. This allows classes that refer to each other:
classVector;// forward declarationclassMatrix{// ...friendVectoroperator*(constMatrix&,constVector&);};classVector{// ...friendVectoroperator*(constMatrix&,constVector&);};and if a particular source file only uses pointers and references to the class, this makes it possible to reduce #include dependencies:
// In MyStruct.h#include<iosfwd> // contains forward declaration of std::ostreamstructMyStruct{intvalue;friendstd::ostream&operator<<(std::ostream&os,constS&s);// definition provided in MyStruct.cpp file which uses #include <ostream>};If forward declaration appears in local scope, it hides previously declared class, variable, function, and all other declarations of the same name that may appear in enclosing scopes:
structs{inta;};structs;// does nothing (s already defined in this scope)voidg(){structs;// forward declaration of a new, local struct "s"// this hides global struct s until the end of this blocks*p;// pointer to local struct sstructs{char*p;};// definitions of the local struct s}Note that a new class name may also be introduced by an
which appears as part of another declaration, but only if
can't find a previously declared class with the same name.
classU;namespacens{classYf(classTp);// declares function ns::f and declares ns::T and ns::YclassUf();// U refers to ::U// can use pointers and references to T and YY*p;T*q;}Member specification
The member specification, or the body of a class definition, is a brace-enclosed sequence of any number of the following:
1) Member declarations of the form
attr(optional)decl-specifier-seq(optional)member-declarator-list(optional);This declaration may declare
and non-static
and
, member
, member
, and
. It may also be a
.
classS{intd1;// non-static data memberinta[10]={1,2};// non-static data member with initializer (C++11)staticconstintd2=1;// static data member with initializervirtualvoidf1(int)=0;// pure virtual member functionstd::stringd3,*d4,f2(int);// two data members and a member functionenum{NORTH,SOUTH,EAST,WEST};structNestedS{std::strings;}d5,*d6;typedefNestedSvalue_type,*pointer_type;};2) Function definitions, which both declare and define
or
. A semicolon after a member function definition is optional. All functions that are defined inside a class body are automatically
, unless they are attached to a
(since C++20).
classM{std::size_tC;std::vector<int>data;public:M(std::size_tR,std::size_tC):C(C),data(R*C){}// constructor definitionintoperator()(std::size_tr,std::size_tc)const// member function definition{returndata[r*C+c];}int&operator()(std::size_tr,std::size_tc)// another member function definition{returndata[r*C+c];}};3)
public:, protected:, and private:
classS{public:S();// public constructorS(constS&);// public copy constructorvirtual~S();// public virtual destructorprivate:int*ptr;// private data member};4)
:
classBase{protected:intd;};classDerived:publicBase{public:usingBase::d;// make Base's protected member d a public member of DerivedusingBase::Base;// inherit all bases' constructors (C++11)};5)
declarations:
template<typenameT>structFoo{static_assert(std::is_floating_point<T>::value,"Foo<T>: T must be floating point");};6)
:
structS{template<typenameT>voidf(T&&n);template<classCharT>structNestedS{std::basic_string<CharT>s;};};(since C++11)8)
of member class templates:
structS{template<classCharT>structNestedS{std::basic_string<CharT>s;};template<classCharT>NestedS(std::basic_string<CharT>)->NestedS<CharT>;};(since C++17)(since C++20)Local classes
A class declaration can appear inside the body of a function, in which case it defines a local class. The name of such a class only exists within the function scope, and is not accessible outside.
Members of a local class can only be declared in the definition of that class, except that members that are
can also be declared in the nearest enclosing
of that class.
A class nested within a local class is also a local class.
A local class cannot have static data members.
Member functions of a local class have no linkage.
Member functions of a local class have to be defined entirely inside the class body.
Local classes other than
(since C++14) cannot have member templates.
Local classes cannot have
.
Local classes cannot define
inside the class definition.
A local class inside a function (including member function) can access the same names that the enclosing function can access.
Local classes could not be used as template arguments.
(until C++11)Run this code
#include<algorithm>#include<iostream>#include<vector>intmain(){std::vector<int>v{1,2,3};structLocal{booloperator()(intn,intm){returnn>m;}};std::sort(v.begin(),v.end(),Local());// since C++11for(intn:v)std::cout<<n<<' ';std::cout<<'\n';}Output:
3 2 1 Keywords
,
,
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 member declarations could not be empty empty declaration allowed
C++98 member-declarator-list could be empty when decl-specifier-seq
contains a storage class specifier or cv qualifier the list must not be empty
C++98 it was unclear where the members of nested classes can be declared made clear See also