[英]What does "class classname* funcname(void) "mean in C++?
I found the following code in a header file and the "BOOT" class is defined in another header file.我在头文件中找到了以下代码,而“BOOT”类在另一个头文件中定义。
class BOOT* boot(void);
It looks like a declaration of a function, but it begins with class
.它看起来像一个函数的声明,但它以
class
开头。
This is an elaborated type specifier:这是一个详细的类型说明符:
Elaborated type specifiers may be used to refer to a previously-declared class name (class, struct, or union) or to a previously-declared enum name even if the name was hidden by a non-type declaration.
精心设计的类型说明符可用于引用先前声明的类名(类、结构或联合)或先前声明的枚举名称,即使该名称被非类型声明隐藏。 They may also be used to declare new class names.
它们也可用于声明新的类名。
https://en.cppreference.com/w/cpp/language/elaborated_type_specifier https://en.cppreference.com/w/cpp/language/elaborated_type_specifier
Taking from answers of Artefacto and dfrib because it brings it on point: It is equivalent to:取自 Artefacto 和 dfrib 的答案,因为它指出了这一点:它相当于:
class BOOT;
BOOT* boot(void);
In your example it essentially does a forward declaration of the class BOOT if it is not known yet.在您的示例中,如果尚不知道,它本质上会执行 BOOT 类的前向声明。 See this example
struct Data* Data;
请参阅此示例
struct Data* Data;
from the same page:从同一页面:
struct Node {
struct Node* Next; // OK: lookup of Node finds the injected-class-name
struct Data* Data; // OK: declares type Data at global scope
// and also declares the data member Data
friend class ::List; // error: cannot introduce a qualified name
enum Kind* kind; // error: cannot introduce an enum
};
Data* p; // OK: struct Data has been declared
It is the same as this:它与此相同:
class BOOT;
BOOT* boot(void);
So it's a pointer to class BOOT
, but with a declaration of the class as well.所以它是一个指向
class BOOT
的指针,但也带有类的声明。 The class need not be defined at this point.此时不需要定义类。
What does “class classname* funcname(void) ”mean in C++?
“class classname* funcname(void)”在 C++ 中是什么意思?
It is a function declaration.它是一个函数声明。
It looks like a declaration of a function, but it begins with "class".
它看起来像一个函数的声明,但它以“类”开头。
class classname*
is the return type of the function. class classname*
是函数的返回类型。 class classname
is an elaborated type specifier. class classname
是一个详细的类型说明符。
In C++ you may declare a function type whose return type contains a class type that is defined elsewhere, as long as you either explicitly forward declare the class type prior to the function declaration:在 C++ 中,你可以声明一个函数类型,它的返回类型包含一个在别处定义的类类型,只要你在函数声明之前显式地向前声明类类型:
class BOOT;
BOOT* boot();
but you may likewise place the forward declaration in-line in the function declaration:但您也可以将前向声明内嵌在函数声明中:
class BOOT* boot();
This is one of the places where, possibly somewhat unexpectedly, forward declarations can be used.这是可以使用前向声明的地方之一,可能有点出乎意料。 Another example is as in the template argument list for a type-template parameter:
另一个例子是类型模板参数的模板参数列表:
template<typename T>
struct Identity { using type = T; };
using IdentityBoot = Identity<struct BOOT>;
// ^^^^^^^^^^^ also a forward declaration
// OK
BOOT* boot();
// OK
typename IdentityBoot::type* another_boot();
Formally, it's an elaborated-type-specifier , governed by [dcl.type.elab] ,正式地说,它是一个详细的类型说明符,由[dcl.type.elab] 管理,
elaborated-type-specifier: class-key [...] [...]
that is used to, as per [class]/1 , make a class-name that is introduced into the scope where the elaborated-type-specifier is used [ emphasis mine]:根据[class]/1 ,用于创建一个类名,该类名被引入到使用详细类型说明符的范围中 [强调我的]:
A class is a type.
类是一种类型。 Its name becomes a class-name ([class.name]) within its scope.
它的名称成为其范围内的类名([class.name]) 。 [...]
[...]
Class-specifiers and elaborated-type-specifiers are used to make class-names .
类说明符和详细类型说明符用于制作类名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.