函数签名中的'class'关键字 - 它是标准的C ++吗? [重复]

时间:2022-12-22 22:30:19

This question already has an answer here:

这个问题在这里已有答案:

I'm studying code given to me and I see:

我正在研究给我的代码,我看到:

AFPSGameMode::AFPSGameMode(const class FPostConstructInitializeProperties& PCIP)
    : Super(PCIP) {   }

I'm specially curious about the use of the class keyword. Is this standard C++ and if so what does it mean?

我特别好奇使用class关键字。这是标准的C ++,如果是这样,它意味着什么?

Thank you.

2 个解决方案

#1


5  

The class keyword is allowed here, it's just rare to see it placed here since it can either be completely omitted (if this class has been previously declared) or replaced with the forward declaration:

这里允许使用class关键字,因为它可以被完全省略(如果此类已经被声明)或者被前向声明替换,所以很少看到它放在这里:

void foo(const class FPostConstructInitializeProperties& p){ ... }

which is equivalent to:

这相当于:

class FPostConstructInitializeProperties; // <-- forward declaration
void foo(const FPostConstructInitializeProperties& p){ ... }

Don't get confused with the weird naming conventions. The snippet you have provided expresses something like this:

不要混淆奇怪的命名约定。您提供的代码段表达了以下内容:

class B{
public:
    B(){ }
    B(const B& b){ };
};

class A{
public:
    B my_b;
    A(const class B& b) : my_b(b) { }  // <-- class keyword in ctor's param decl.
};

that could be used for example like this (but I guess it's clear enough already):

可以像这样使用(但我想它已经足够清楚了):

int main() {
    B b;
    A a(b);
}

#2


1  

As it was old C, if you have struct say,

因为它是老C,如果你有结构说,

struct account
{
int field;
..
};

You can use it for creating its variables (objects) like,

你可以用它来创建它的变量(对象),比如

account obj;

or,

struct account obj;.

struct account obj;。

Same is for class, you may use it, or avoid it. But it is usually not used, but permitted.

对于课程也是如此,您可以使用它,或者避免使用它。但通常不使用,但允许使用。

#1


5  

The class keyword is allowed here, it's just rare to see it placed here since it can either be completely omitted (if this class has been previously declared) or replaced with the forward declaration:

这里允许使用class关键字,因为它可以被完全省略(如果此类已经被声明)或者被前向声明替换,所以很少看到它放在这里:

void foo(const class FPostConstructInitializeProperties& p){ ... }

which is equivalent to:

这相当于:

class FPostConstructInitializeProperties; // <-- forward declaration
void foo(const FPostConstructInitializeProperties& p){ ... }

Don't get confused with the weird naming conventions. The snippet you have provided expresses something like this:

不要混淆奇怪的命名约定。您提供的代码段表达了以下内容:

class B{
public:
    B(){ }
    B(const B& b){ };
};

class A{
public:
    B my_b;
    A(const class B& b) : my_b(b) { }  // <-- class keyword in ctor's param decl.
};

that could be used for example like this (but I guess it's clear enough already):

可以像这样使用(但我想它已经足够清楚了):

int main() {
    B b;
    A a(b);
}

#2


1  

As it was old C, if you have struct say,

因为它是老C,如果你有结构说,

struct account
{
int field;
..
};

You can use it for creating its variables (objects) like,

你可以用它来创建它的变量(对象),比如

account obj;

or,

struct account obj;.

struct account obj;。

Same is for class, you may use it, or avoid it. But it is usually not used, but permitted.

对于课程也是如此,您可以使用它,或者避免使用它。但通常不使用,但允许使用。