Download

Class keyword in member declaration

Greetings,

This is more of a c++ question but I dont know even the name of this subject to search in Google so please bear with me.

What does the class keyword do in a member declaration? For example,

public: class USomething* Attribute;

Thank you in advance.

Sounds like a homework question :slight_smile:

​​

Forward dec;aration. Mainly used for optimize compilation time and to avoid circular dependency between header files.

Actually it isn’t. I just found this line in a Youtube video and did not understand what it means. In other languages like Object Pascal for instance, it would mean a public class member pointer (or static member), but I guess it is something different in C++.

If you could tell me the name of this C++ language resource I could look for it in Google. So far I have found only things like common class declarations or instance pointers, which I already know.

Thank you for your answer anyway,

Sorry to ask again but I searched for forward declarations in C++ and I only got results like so:

class A;
class B { A* someMember; }
class A { … }

It makes sense and is similar to other languages I know. But in the example I mentioned in my first post, the class keyword is used inside the B declaration. In the example above, it would be:

class B {
class A* someMember;
}

Edit (typo): Is it still a forward declaration? I think it’s strange because the class was UStaticMeshComponent, which was referenced in the includes.

class could also be for disambiguation.

example from cppreference:



int main()
{    
    Bar Bar(1);    
    class Bar Bar2(2); // elaborated type
}


Since the first variable is called Bar, the second time you write Bar to declare a Bar you need the keyword class to disambiguate and specify this is a type, otherwise you get an error because the expression “variable_name undeclared_var_name(2)” have no meaning.

Oh, I see! Insteresting enough, there is no class called UStaticMeshComponent in the video source, so I guess it was unnecessary.

Thank you for clarification and for the site. I added it to my bookmark list.

Yes, putting class or struct before a member declaration is just an inline forward declaration. Your two examples are equivalent.

As to why UStaticMeshComponent was forward declared and included in the code you found, maybe the variable was originally forward declared when it was the only reference to the class in the file, but then the header needed to be included for some other usage later.