c++ – The word class in a function argument


The first time I met this construction, when the class is in the function parameters and I can’t understand what it is used for?

virtual void SetupInputComponent(class UInputComponent* InputComponent) override;


This thing ( {class|struct|union|enum} имя ) is called an elaborated type specifier .

There is almost no difference between UInputComponent and class UInputComponent .

1. Naturally, if you write not a class to the right of the class , but another type, this is an error.

2. If UInputComponent has not yet been declared, then class UInputComponent does not cause an error, but declares the class in the current namespace (even if we are inside the class, it is still not declared in it, but in the nested namespace itself).

This only works if the name is unqualified (does not contain :: ). If it is qualified and such a class has not yet been declared, it is an error.

3. Elaborated type specifier is needed when the class name matches the name of something else:

class A {};

void foo()
    int A;

    A ptr; // error: must use 'class' tag to refer to type 'A' in this scope
    class A ptr; // ok

4. Elaborated type specifier removes the need for typename in templates:

struct A
    class B{};

template <typename T = A> struct C
    T::B y; // error: missing 'typename' prior to dependent type name 'T::B'
    typename T::B x; // ok
    class T::B y; // ok

C c;

But typename works for all types, while class only works for classes. Don't confuse an elaborated type specifier with a template parameter declaration: there is no difference between template <typename T> and template <class T> .

Scroll to Top