class' keyword in variable definition in C++
An elaborated type specifier is a type name preceded by either the class, struct, enum, or union keyword.
class identifier
struct identifier
enum identifier
union identifier
An elaborated type specifier is used either for emphasis, or to reveal a type name that is hidden by the declaration of a variable with the same name in the same scope.
source
Declaring a variable with class keyword vs Declaring one without class keyword in function signatures
Using keyword class, struct, enum in a type parameter declaration is called elaborated type specifier. It introduces the new type in the scope where the function is declared.
It is similar to forward declaration.
There is also another using of such a declaration. For example if a name of an object or a function name hides a class or enum with the same name. For example
struct A {};
A A; // now A is seen as an identifier of the object
void f( struct A );
What is the class keyword before a function argument?
If a function or a variable exists in scope with the name identical to the name of a class type, class can be prepended to the name for disambiguation, resulting in an elaborated type specifier.
You are always allowed to use an elaborated type specifier. Its major use case, however, is when you have a function or variable with an identical name.
Example from cppreference.com:
class T {
public:
class U;
private:
int U;
};
int main()
{
int T;
T t; // error: the local variable T is found
class T t; // OK: finds ::T, the local variable T is ignored
T::U* u; // error: lookup of T::U finds the private data member
class T::U* u; // OK: the data member is ignored
}
What is the class keyword without a body for?
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.
Use the keyword class as a variable name in C++
try something like this:
#define class class_variable
// if class is only used in declarations, you can also do
// #define class
#include "c.h"
#undef class
it may cause problems, but maybe worth a shot
alternative (using Makefile):
python_.hpp: /usr/include/python.h
perl -pe 's/\Wclass\W//g' $< > $@
...
#include "python_.hpp"
what is 'class' in 'class DataType* Variable' in Unreal Engine Shooter Game Sample
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.
Why use 'struct' keyword in class pointer declaration in C++
There's rarely a reason to do this: it's a fallover from C and in this case the programmer is simply being sentimental - perhaps it's there as a quest for readability. That said, it can be used in place of forward declarations.
In some instances you might need to disambiguate, but that's not the case here. One example where disambiguation would be necessary is
class foo{};
int main()
{
int foo;
class foo* pf1;
struct foo* pf2;
}
Note that you can use class
and struct
interchangeably. You can use typename
too which can be important when working with templates. The following is valid C++:
class foo{};
int main()
{
class foo* pf1;
struct foo* pf2;
typename foo* pf3;
}
Clang C Compiler 'class' keyword reserved?
It's completely fine in C. When you build that as C++, you encounter an error because class
is a C++ keyword.
As far as fixing it, you would normally choose an identifier other than class
. However, ffmpeg developers may not be so agreeable with that change. Therefore, you may need to either:
- restrict the visibility of that header to C translations
- or edit your own copy in order to use it in C++ translations
Fortunately, you are also using a C compiler which has good support of C99 features in this case. C Compilers which do not support C99 well are particularly troublesome with ffmpeg sources (because you would then compile the whole program as C++ for the C99 features, and the conflict count would be much higher).
(there are other dirty tricks you could do to try to work around the problem, but i will not mention them)
Can there be anything between keyword 'class' and class name in c++?
If you saw it on a Windows code, this is probably a macro which determine if you want to export or import the given class.
It's very common if you are dealing with dll-s.
So, this macro is probably something like this:
#ifdef PROJECTNAME_EXPORTS
#define MACROBEFORECLASSNAME __declspec(dllexport)
#else
#define MACROBEFORECLASSNAME __declspec(dllimport)
#endif
If you compile the dll, the PROJECTNAME_EXPORTS preprocessor definition should be defined, so the compiler will export the given class.
If you compile a project which is just using the given dll, the ...EXPORTS won't be defined, so the compiler will import the given class.
C++ syntax question, use of the class keyword
AFAIK
class X x;
is equivalent to
X x;
(EDIT: for those nitpickers: at least, in your case, when X is a previously defined class and there is no other variable named X in the same scope).
In plain-old C, whenever you define a struct
without a typedef
, you have explicitly use the struct
keyword when creating variables. In C++, one can omit the struct
keyword, but you also can write struct X x;
if you prefer that. And since the only difference between class
and struct
in C++ is the default visibility, one can conclude that it is also legal to write class X x;
Answer to your edit:
class Z;
is a forward declaration (often used in header files where you don't want to #include "Z.hpp"
). This is also called an incomplete type declaration. It allows pointers to Z to be declared, for example
class Z *z;
is legal code, even when the compiler has not seen any class body declaration of Z. What is not allowed is to create an instance of z like class Z z;
as long as Z is incomplete from the compiler's view.
The code Z z;
, however cannot be interpreted as a forward declaration by the compiler (not even as a disallowed forward declaration). It just shows up as "Z is undefined".
Related Topics
Fast Multiplication/Division by 2 for Floats and Doubles (C/C++)
Const Method That Modifies *This Without Const_Cast
Threadsafe Vector Class for C++
Convert Std::String to Qstring
#Error Please Use the /Md Switch for _Afxdll Builds
Hide a C++ Code Block from Intellisense
What Is the Array Form of 'Delete'
Does the Unary + Operator Have Any Practical Use
Concurrent Writes in the Same Global Memory Location
How to Show Qsqlquerymodel in Qml