Declaring functions
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
一个函数的声明中引入函数的名字和类型。它可能会出现在任何范围内,通常放置在头文件中.
原文:
A function declaration introduces the function name and its type. It may appear in any scope, and is commonly placed in header files.
ret name ( params ) ;
|
(1) | ||||||||
ret name ( params ) cv ref except attr ;
|
(2) | ||||||||
auto name ( params ) cv ref except attr -> ret ;
|
(3) | (C++11 起) | |||||||
一个函数的定义提供了一个函数体。它只能出现在命名空间或类范围.
原文:
A function definition provides the body of a function. It may only appear in namespace or class scope.
decl name ( params ) { body }
|
(4) | ||||||||
attr decl name ( params ) cv ref except attr virt try init-list { body } catch
|
(5) | ||||||||
attr decl name ( params ) cv ref except attr -> ret virt try init-list { body } catch
|
(6) | (C++11 起) | |||||||
attr decl name ( params ) cv ref except attr virt = 0 ;
|
(7) | ||||||||
attr decl name ( params ) cv ref except attr virt = default ;
|
(8) | (C++11 起) | |||||||
attr decl name ( params ) cv ref except attr virt = delete ;
|
(9) | (C++11 起) | |||||||
[编辑] 解释
1)
典型的函数声明
2)
综合函数的声明
3)
综合尾随返回类型的函数声明
原文:
Comprehensive function declaration with trailing return type
4)
典型的非成员函数的定义
原文:
Typical non-member function definition
5)
全面的功能定义
6)
综合尾随返回类型的函数定义
原文:
Comprehensive function definition with trailing return type
7)
纯虚成员函数
8)
拖欠的成员函数
9)
删除的成员函数
attr(C++11) | - | |||||
ret | - | 由该函数返回的类型,可能是void如果该函数不返回任何内容。不能为数组或函数类型,虽可这样的指针或引用。除了构造函数,析构函数和转换运算符的返回类型不能提供所需的所有功能.
原文: the type returned by the function, may be void if the function returns nothing. Cannot be array or function type, although can be a pointer or reference to such. Required for all functions except constructors, destructors, and conversion operators, which must not provide a return type. | ||||
decl | - | 声明说明符序列,其中包括没有或以下关键字:静态的,外部,内联,virtual,explicit,friend,constexpr,结合的返回类型,ret
{{par | cv | 可选const,volatile,或const volatile,只适用于非静态成员函数。对于一个成员函数,类
T ,[CPP /语言/这类型的原文: Optional const, volatile, or const volatile, only applicable to non-static member functions. For a member function of class T , the type of the [[cpp/language/this}}|this pointer]] will be const T*, volatile T*, or const volatile T* respectively. A member function declared const cannot modify members of *this. | ||||
ref(C++11) | - | 可选&或&&,只适用于非静态成员函数以外的构造函数或析构函数。类的成员函数
T ,隐含对象的类型参数的重载决议的目的是T&或T&&:一个的&&合格的成员函数可以被称为一个rvalue对象的表达原文: Optional & or &&, only applicable to non-static member functions other than constructors or destructors. For a member function of class T , the type of the implicit object parameter for the purpose of overload resolution will be T& or T&& respectively: a &&-qualified member function can only be called on an rvalue object expression | ||||
except | - | |||||
virt(C++11) | - | 可选override或final,只适用于非静态成员函数
原文: Optional override or final, only applicable to non-static member functions | ||||
-> ret(C++11)
|
- | 后返回类型,仅适用于ret是auto。类型依赖于参数的名称,如template <class T, class U> auto add(T t, U u) -> decltype(t + u);有用的,如果是复杂的,如在auto fpif(int)->int(*)(int)
原文: Trailing return type, only applicable if ret is auto. Useful if the type depends on argument names, such as template <class T, class U> auto add(T t, U u) -> decltype(t + u); or is complicated, such as in auto fpif(int)->int(*)(int) | ||||
init-list | - | |||||
try
|
- | 可选的功能try块中。如果存在,catch必须被提供
原文: Optional function try block. If present, catch must be provided | ||||
catch | - | |||||
body | - | 身体的功能,一个复合语句(可能为空)
原文: The body of the function, a (possibly empty) compound statement | ||||
params | - | 参数列表
原文: The list of parameters
|
[编辑] 例1:非成员函数
#include <iostream> #include <string> // declaration in namespace(file) scope // (the definition is provided later) int f1(); // simple function with a default argument, returning nothing void f0(const std::string& arg = "world") { std::cout << "Hello, " << arg << '\n'; } // function returning a pointer to f0 auto fp11() -> void(*)(const std::string&) { return f0; } // function returning a pointer to f0, pre-C++11 style void (*fp03())(const std::string&) { return f0; } int main() { f0(); fp11()("test"); fp03()("again"); int f2(std::string); // declaration in function scope std::cout << f2("bad12") << '\n'; } // simple non-member function returning int int f1() { return 42; } // function with an exception specification and a function try block int f2(std::string str) noexcept try { return std::stoi(str); } catch(const std::exception& e) { std::cerr << "stoi() failed!\n"; return 0; }
输出:
Hello, world Hello, test Hello, again stoi() failed! 0
[编辑] 实施例2:成员函数
#include <iostream> #include <string> #include <utility> #include <exception> struct S { int data; // simple converting constructor (declaration) S(int val); // simple explicit constructor (declaration) explicit S(std::string str); // const member function (definition) virtual int getData() const { return data; } }; // definition of the constructor S::S(int val) : data(val) { std::cout << "ctor1 called, data = " << data << '\n'; } // this constructor has a catch clause S::S(std::string str) try : data(std::stoi(str)) { std::cout << "ctor2 called, data = " << data << '\n'; } catch(const std::exception&) { std::cout << "ctor2 failed, string was '" << str << "'\n"; throw; // ctor's catch clause should always rethrow } struct D : S { int data2; // constructor with a default argument D(int v1, int v2 = 11) : S(v1), data2(v2) {} // virtual member function int getData() const override { return data*data2; } // lvalue-only assignment operator D& operator=(D other) & { std::swap(other.data, data); std::swap(other.data2, data2); return *this; } }; int main() { D d1 = 1; S s2("2"); try { S s3("not a number"); } catch(const std::exception&) {} std::cout << s2.getData() << '\n'; D d2(3, 4); d2 = d1; // OK: assignment to lvalue // D(5) = d1; // ERROR: no suitable overload of operator= }
输出:
ctor1 called, data = 1 ctor2 called, data = 2 ctor2 failed, string was 'not a number' 2 ctor1 called, data = 3