/// 1
class UMyClass* MyClass;
/// 2
UMyClass* MyClass;
This method may work either or only one of them. I’m confused about what’s different.
/// 1
class UMyClass* MyClass;
/// 2
UMyClass* MyClass;
This method may work either or only one of them. I’m confused about what’s different.
One (1) is a forward declaration of the class while the other (2) is just a normal class reference.
Both will work if you’ve included the MyClass header file, but the first would be redundant.
A forward declaration (also can be written prior as: class UMyClass;) simply lets the compiler know that the class you’re talking about exists. They’re only used when you don’t want to include the header file for that class (include MyClass.h).
The compiler doesn’t know anything about the class so you won’t be able to do anything with that reference in this class, but it will still be a valid pointer that other classes can use without casting.
They’re great for holder objects if you use them- imagine having a holder object that include s all of their dependencies. Then all of the classes that use that object would also be including every single header file- even if they didn’t need them.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.