Search code examples
c++unreal-engine4

what is 'class' in 'class DataType* Variable' in Unreal Engine Shooter Game Sample


I recently downloaded the Shooter Game for Unreal 4 Engine and I am just trying to pick apart the c++ but my c++ isn't the best I notice a variable called

class AShooterCharacter* MyPawn; 

Set in the header file for ShooterWeapon.h

I am trying to understand what the class part is.

[Edit] I notice people downed my question so I changed it to one question. I hope people are willing to help rather then degrade my question. Theres no such thing as a dumb question :)... Especially in programming


Solution

  • If AShooterCharacter is already in scope, then it probably means basically nothing.

    class AShooterCharacter* MyPawn;
    // ^ the same as just:
    // AShooterCharacter* MyPawn;
    

    In C, when naming structure types, you had to use the struct keyword:

    struct Foo
    {
       int x, y, z;
    };
    
    struct Foo obj;
    

    In C++, you don't need to do that because Foo becomes a nameable type in its own right:

    Foo obj;
    

    But you still can write struct Foo if you want to.

    The same applies for class, just as a consequence of how the language grammar and semantics are defined.

    There are only two times when it makes a difference.

    Usage 1: Disambiguation (of sorts)

    You can use it to specify that you want to refer to an in-scope type name when it is otherwise being hidden by some other name, e.g.:

    class Foo {};
    
    int main()
    {
       const int Foo = 3;
    
       // Foo x;     // invalid because `Foo` is now a variable, not a type
       class Foo x;  // a declaration of a `Foo` called `x`;
    }
    

    But if you find yourself "needing" this then, in my opinion, you have bigger problems!

    Usage 2: Forward declaration

    Otherwise, it is the same as doing this:

    class Foo;   // a forward declaration
    Foo* x;
    

    Arguably it saves a line of code if you are going to forward declare the type and immediately declare a pointer to an object of that type.

    It's not common style, though.