What does "class className;" by itself do in C++? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ - Forward declaration
so in my header file i have a class defined/declared with all it features named User
then in a .cpp source file there is near the top class User;
im new to c, but couldnt find an answer in the few tutorials i looked into so came here
what does it do ?
thankyou.

It's called a forward declaration and enables the compiler to recognise the class without actually knowing its internals. You just inform the compiler that such a class exists, and declaring a pointer to it will not raise an error.
The alternative would be to include the corresponding header file that declares the class, but that would be way too heavy if all you need is having pointers to that type.

It is a so called "Forward declaration": it makes the class known to the compiler, without actually defining it. Note that this is C++, not C.

It declares the class without actually defining it. In that sense, it's no different to:
void doSomething(void);
That prototype tells the compiler that doSomething() exists but doesn't actually specify what it does.
It's generally used where you need the class to exist (so you can reference it somehow) but you don't need to know anything about its properties.

Related

declaring class inside a header file in C++ [duplicate]

This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 5 months ago.
I am trying to understand the src code of FESVR in RISCV, I often come across using of class <class_name> inside a header file like the code linked below
https://github.com/riscv-software-src/riscv-isa-sim/blob/master/fesvr/elfloader.h
I know you can define a whole class inside a header file and include that header file inside a c++ file and use the object directly, but the code above does not completely define the class or create an object it just declares it.
Can anyone explain, what does declaring a class like this achieves?
It's called a forward declaration.
When you're only declaring functions, fields, etc., which use a reference or a pointer to a certain type, you don't necessarily have to know the complete type in order for the compiler to make sense of your code. It just needs to know that the type exists, you did not make any spelling mistakes, etc. E.g. at that point the type can be incomplete and a forward declaration is sufficient. This saves you from having to include the header file which declares the entire type, which saves compile time. Additionally it can break cyclic dependencies.
Only at the place where the type is actually used (in your case most likely in the .cpp file), the compiler must know whether the type actually has the fields or methods you're referring too, it must know the size of the type, etc.. E.g. the type must be complete at that point.
Use forward declarations wherever possible.
See:
C++ class forward declaration
https://en.wikipedia.org/wiki/Forward_declaration

What does a semicolon after a class name do? [duplicate]

This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 8 years ago.
I've noticed lines of code like this in the book I'm reading:
namespace sf
{
class RenderWindow;
}
class StateStack;
class Player;
class State
{
// Code for the class
};
What do the lines with just the class, classname, and semicolon mean?
These are forward declarations. They let the following code know that there are classes with the names RenderWindow, StateStack, and Player. This satisfies the compiler when it sees these names used. Later the linker will find the definition of the classes.
It is a forward declaration, essentially it signals to the compiler that the full definition will follow elsewhere.
The primary use case for this is cases where you do not need the full definition, for example if you have a pointer of type T you don't need the full definition of T until instantiation and thus its not required to have for a declaration of T*.

What is the use of class declaration in c++ [duplicate]

This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 8 years ago.
I was going through a c++ code and saw a header file with below declaration:
file a.h
class xyz;
But that class was not defined any where in the file. I also wrote a code and it perfectly compiles fine. Just wanted to know what is the use of declaring a class like this as it does't enforces programmer to define.
Thanks in advance!!
this is a forward declaration of a class.
you can use xyz* variables in the header file - the linker will resolve the actual class implementation later
That's a forward declaration (or simply declaration) and it allows you to use the name xyz in contexts where a full definition isn't required.
One example is when you have another class which contains a pointer to a xyz:
class xyz;
class Aux
{
xyz* k;
};
Other uses are return types, parameter types and references.

Forward declaration or complete definition required [duplicate]

This question already has answers here:
When can I use a forward declaration?
(13 answers)
In C++, I want two classes to access each other
(1 answer)
Closed 9 years ago.
Although i have been using forward declaration for a considerable amount of time but never gave a thought to it seriously (my mistake)
Would be helpful if someone could give me pointers or any link regarding following queries :
How do we determine if we require a forward declaration or include is required ?
How does compiler actually works in such cases ? any overhead ?
Suppose i have two interdependent classes A and B. Both of them uses objects of each other, do i need to forward declare in both the classes.
I'd say that:
If you only need a reference or a pointer to a class in the header -> use forward declaration.
The overhead with an include is that the compiler will see more dependencies for the current header, and will thus recompile your c++ body file perhaps unneccesarily.
If possible yes.
A forward declaration can always be used, when the compiler only needs to know that some object exists, but doesn't need it's size or other details iof the object.
For example in some .h file you have:
MyClass *foo(MyClass *p);
In example1.cpp:
// No include needed, forward declaration is ok.
MyClass *foo(MyClass *p)
{
foo2(p);
return p;
}
In example2.cpp:
// include needed, forward declaration is not ok.
MyClass *foo(MyClass *p)
{
p->ClassFkt();
return p;
}
In example3.cpp:
// include needed, forward declaration is not ok.
MyClass *foo(MyClass *p)
{
MyClass *n = new MyClass();
foo2(p, n);
return n;
}
Here you have a prototype of a function which takes a pointer. A pointer is always of known size, so a forward declaration can be used here.
As long as you only use the pointer, for example to pass it on, to some other function, you don't need the include. If you try to access some members or do other operation which requires to know more details, you need to include the header file as well. In the above example1 you wouldn't need the full declaration and the forward declaration is sufficient. In the secaond example it is not, because the compiler doesn't know if your class has the function which is invoked. In the third example only pointers are used, but because of the new the size needs to be known, so a forward declaration is not enough.
In order to avoid extra header depencies, you can use the forward declaration in the .h file in the above example, and do the include only in the cpp file itself.

Why other class names are specified above a class? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When to use forward declaration?
What is the use of specifying other class names above a particular class?
class A;
class B;
class C
{
...
...
};
It's a forward declaration.
As a hangover from C, the C++ compiler works in a top-to-bottom fashion (more or less). If you want your class C to refer to class A or class B in some way (via a pointer or a reference), then the compiler must know about their existence beforehand.
That is called a forward declaration. It allows you to declare a pointer to the type without including its definition. Of course, since the definition doesn't exist at this pointas far as the compiler is concerned, you can only declare pointers or references to the type as the compiler would not know how to construct an object of said type, i.e., it cannot not determine its size.
By forward declaring classes like that, you are telling the compiler that the classes exist, and not to worry, you will link them in later.
This is instead of including the header files with the full definition, and is sufficient if the compiler doesn't need to know what the class looks like. For example if you are only using pointers or references to the class in the current file.
This is called forward declaration and allows the use of those classes in the defined class's prototype.