3

In Unreal Project I have this line:

class USpringArmComponent* CameraBoom

and

class UCameraComponent* FollowCamera

And I haven't ever seen this syntax in c++. What does it mean?

  • 1
    What is strange for you, exactly? Did you know what pointers are, in C++? If not: consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Aug 04 '20 at 12:47
  • What part is strange to you? The pointers or the use of the word `class`? In either case I would suggest read more about C (read about `structs` instead of `classes`) because both these come from C. – Roy Avidan Aug 04 '20 at 12:50
  • 4
    This is a dark corner of the language - difficult to search for if you don't know the term. Downvotes are harsh IMHO. – Bathsheba Aug 04 '20 at 12:51
  • I agree with @Bathsheba, this is a good question. – Frodyne Aug 04 '20 at 12:56
  • 1
    The `*` means the type is a _pointer_, and the _pointer_ may point to a `USpringArmComponent` type (in the first case) for the variable `CameraBoom`, or to a `UCameraComponent` type (in the second case) for the variable `FollowCamera`. – Eljay Aug 04 '20 at 12:58

2 Answers2

6

It's an elaborated type specifier:

https://en.cppreference.com/w/cpp/language/elaborated_type_specifier

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

That's just telling the compiler that UCameraComponent is a class. Not anything else. It's like in C you put struct before any structure variable declarations.

This syntax is useful when you have some messy code (or to convey that it is a class verbosely to the developer).

For example:

class counter
{
// bla bla bla...
};

void foo()
{
    int counter = 0; // Oops someone declared a variable called counter.
                     // How am I going to declare a variable of type `counter`?4
    // counter actual_counter; // Syntax error: expected ';' after expression.
                               //Because counter is a variable
    class counter actual_counter; // You prepend `class` to the deceleration
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Mary Chang
  • 865
  • 6
  • 25
  • I know. I though a beginner friendly approach would be better in this question. – Mary Chang Aug 04 '20 at 12:53
  • Trivial edit to convert the downvote to an upvote. Sorry for trolling. – Bathsheba Aug 04 '20 at 12:59
  • Interesting! I did not know that! Because I don't normally do that kind of thing. I was able to do it this way: `class std::basic_string, std::allocator > s;` But it doesn't parse with the typedef `class std::string s;`. – Eljay Aug 04 '20 at 13:00