1

I don't understand what "class" means in Unreal C++

class USpringComponent* BoomArt;
class UCameraComponent* Camera;

As I know the class must have a body, but there is no such thing. Why write a class? What is the general logic of this key word?

I'm used to seeing this:

class MyClass
{
   // body...
}

Why write the class keyword ahead in Unreal C++? Yet again:

**class** USpringComponent* BoomArt;
SamuelRobb
  • 25
  • 4

2 Answers2

1

You can forward declare a type without fully defining it. There are limiations on how a forward ddeclared tpye can be used but defining a pointer is one valid use case.

The line

class USpringComponent* BoomArt;

is equilvalent to

class USpringComponent;    // Forward declaration of the class
USpringComponent* BoomArt; // Define a pointer to an object of the class.

Related: When can I use a forward declaration?

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • That is, you can declare absolutely without problems without the class keyword or if desired, with it? – SamuelRobb Feb 10 '21 at 17:24
  • @SamuelRobb The forward declartion must use the keyword `class`. The variable definition can omit the keyword `class` only if the `class` has been forward declared first. – R Sahu Feb 10 '21 at 17:31
1
class USpringComponent* BoomArt;

Declares a variable named BoomArt. The type is a USpringComponent*, or at least that's what we want the type to be.

Presumably, class USpringComponent has no been declared yet, meaning if we wrote USpringComponent* BoomArt, we'd get a compilation error ("Unknown type USpringComponent" or something along those lines).

Putting class out front just tells the compiler that, even though you haven't defined anything named USpringComponent yet, there is a class by that name somewhere.

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30