Forward declaring and using in one step - c++

As an optimisation, or to avoid include looping, a type may be forward declared, This leads to code like:
class A;
class B
{
A *a;
};
If the number of forward declaration becomes large, it can take up a lot of space at the top of the header file. Is there a way of forward declaring and using at the same time? Sort of like:
class B
{
extern A *a;
};
I've never really thought about this before, but I have a header with a bunch of forward declarations and I would like to make it tidier (without farming them off to another include file).
EDIT: I changed 'a' to a pointer, as it was rightly pointed out that you can only use forward declare on pointers and references.

What you're asking isn't completely clear but, if I understand you right, you can forward declare at the same time as declaring your variables:
class B
{
class A* a; // declaring A as class is an in-place forward declaration
};
Is that what you mean?

A forward declaration wouldn't allow you to do
class A;
class B
{
A a;
};
unless A is a reference or pointer type, since a forward declaration doesn't give any additional information on the size of the object (unless for enum class in C++11). So are you using pointers/references? Otherwise it means you are including the definition of A for sure.
Regarding your problem there is no way to forward declare and use a type since we're talking about two different things. A variable declaration doesn't define a type, it defines a variable.
A simple solution to your problem would be to gather all forward declaration in a single header file and include it in the project (or in your eventual precompiled header). This wouldn't create too much problems, since forward declarations don't expose anything nor they are heavyweight.

No, you can't do what you want. This answer about forward declarations should give you all the gory details, but in summary you need the full definition of a type if you want to use it (as including more or less does); not just the fact that it exists (as forward declaring more or less does).

Related

C++ (and ROS) - Include vs. forward declare of reference with set default and typedef

I have two very related questions regarding forward declarations, their advantages and difference with #includes. After reading on them it's still unclear to me if:
using a ConstPtr from a ROS message (like this) counts as a pointer and can be (somehow) forward declared, or requires an #include;
void foo(const Eigen::Vector3d& scale={0.001, 0.001, 0.001}); in a .h file would be fine with something like (but this doesn't actually compile)
namespace Eigen
{
class Vector3d;
}
at the top of the .h after all the other #includes or if I should use the proper header.
To be clear, the second issue is with the fact scale has a default value, which is actually the one I will always be using in the .cpp. This is the only instance where I'm using a Vector3d.
I'm also fairly certain if the forward declaration is enough I therefore would not need to include the proper header in the .cpp as well, since I'm only ever using the default value inside the method.
A forward declaration of X is sufficient to use an X* or X& as a function parameter or class member, because the full definition of a class is not needed to be able to use its address.
But in order to create an object of that class, even one with a default value, you're going to need its definition.

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.

Forward Declaration of class in C++, incomplete type

I have an issue with Forward Declaration in C++ using clang compiler. Here is my code. It points data in CReference member as incomplete type.
Please Help
class Internal;
class CReference {
private:
Internal data;
public:
CReference () {}
~CReference (){}
};
class Internal {
public:
Internal () {}
~Internal () {}
};
Forward declarations are useful when the compiler does not need the complete definition of the type. In other words, if you change your Internal data; to Internal* data or Internal& data, it will work.
Using Internal data;, the compiler needs to know the whole definition of Internal, to be able to create the structure of CReference class.
Forward declaration only allows you to use pointers and references to it, until full declaration is available
To use a type as a member of a class the compiler has to know how big it is, so that the size of the class can be correctly calculated. A forward declaration doesn't provide that information (C++ won't look ahead and try to find it, especially since the body might be declared in another translation unit), so you can't use it as a by-value member.
You can use a pointer or a reference instead, because pointers and references are the same size no matter what type they refer to. Then the compiler only needs to know the size of that type once you start manipulating it, and so you can get away without the full declaration until then.
As mentioned above. Forward declaration has it's use to avoid header hell in header files when using only a plain pointer of a class in the header.
You usually want to keep includes in header files as few as possible.
This can be achieved by forward declaring a class, but only if it is not a nested class and only if the pointer is used in header, as for this the pointer size is the required information, which is provided by the forward decl.

class names after namespace in C++

New to C++
there is a namespace i.e. and right after it a couple of class names
namespace abc {
class Cursor;
class BufferAllocator;
....
....
}
What does the above class declaration of Cursor and BufferAllocator do here?
It simply means "these classes exists" in the namespace abc, without providing any informations on their implementations.
It's called forward declarations.
It can be useful for :
Avoiding cycles in header inclusions (When class A has a member of class B, and class B has a member of class A)
Reducing dependencies between classes (because you can have a member pointer to a forward-declared class, but can't have directly a member, as the compiler doesn't know what's the size of the class without its implementation details, but know the size of a pointer). This is used notably in the Pimpl idiom.
(There might be other uses for this, but these are the most obvious that come to mind).
It's a forward declaration. It tells the following code that "there is a class called Cursor. You don't need to know what's in it [because we're only using it as a pointer or reference in the code, until it has been defined]".
Cursor and BufferAllocator are simply being forward-declared in their namespace (so they can be used in pointer/reference contexts).
It's a forward declaration. It can be used to inform the compiler of the existence of types when you're only going to use a pointer or reference to that type. The size of a pointer or reference is invariant of the type that it refers to, so the compiler doesn't need to see the entire definition of the type in that case; it just needs to know that the type exists first.
This can be useful in cases where the header that normally declares the type is large (think headers that include a lot of declarations or template instantiations), in which case it can decrease your compile times (sometimes significantly). You can just forward-declare the type and skip including the header, so your compiler doesn't need to process it.
namespace are helpful in a way they avoid typing particular classname in front of every function.
As you are new you will mostly see using namespace std;
so now you can use cout directly if you do not use this statement then you have to write std::cout for every use of cout
hope this helps

Forward Declaration of a Base Class

I'm trying to create proper header files which don't include too many other files to keep them clean and to speed up compile time.
I encountered two problems while doing this:
Forward declaration on base classes doesn't work.
class B;
class A : public B
{
// ...
}
Forward declaration on STD classes doesn't work.
namespace std
{
class string;
}
class A
{
string aStringToTest;
}
How do I solve these problems?
The first problem you can't solve.
The second problem is not anything to do with standard library classes. It's because you declare an instance of the class as a member of your own class.
Both problems are due to the requirement that the compiler must be able to find out the total size of a class from its definition.
However, the compiler can work out the size of a pointer to a class, even if it doesn't yet have the full definition of it. So a possible solution in such cases is to have a pointer (or reference) member in the consuming class.
Not much help in the base class case, because you won't get an 'is a' relationship.
Nor is it worth doing for something like std::string. Firstly, it's supposed to be a convenient wrapper around a character buffer, to save you from doing memory management on something so simple. If you then hold a pointer to it, just to avoid including the header, you're probably taking a good idea too far.
Secondly (as pointed out in a comment), std::string is a typedef to std::basic_string<char>. So you need to forward declare (and then use) that instead, by which time things are getting very obscure and hard to read, which is another kind of cost. Is it really worth it?
As answered before by Earwicker, you can not use forward declarations in any of those cases as the compiler needs to know the size of the class.
You can only use a forward declaration in a set of operations:
declaring functions that take the forward declared class as parameters or returns it
declaring member pointers or references to the forward declared class
declaring static variables of the forward declared type in the class definition
You cannot use it to
declare a member attribute of the given type (compiler requires size)
define or create an object of the type or delete it
call any static or member method of the class or access any member or static attribute
(did I forget any?)
Take into account that declaring an auto_ptr is not the same as declaring a raw pointer, since the auto_ptr instantiation will try to delete the pointer when it goes out of scope and deleting requires the complete declaration of the type. If you use an auto_ptr in to hold a forward declared type you will have to provide a destructor (even if empty) and define it after the full class declaration has been seen.
There are also some other subtleties. When you forward declare a class, you are telling the compiler that it will be a class. This means that it cannot be an enum or a typedef into another type. That is the problem you are getting when you try to forward declare std::string, as it is a typedef of a specific instantiation of a template:
typedef basic_string<char> string; // aproximate
To forward declare string you would need to forward declare the basic_string template and then create the typedef. The problem is that the standard does not state the number of parameters that basic_string template takes, it just states that if it takes more than one parameter, there rest of the parameters must have a default type so that the expression above compiles. This means that there is no standard way for forward declaring the template.
If, on the other hand you want to forward declare a non-standard template (non STL, that is) you can do it for as long as you do know the number of parameters:
template <typename T, typename U> class Test; // correct
//template <typename T> class Test; // incorrect even if U has a default type
template <typename T, typename U = int> class Test {
// ...
};
At the end, the advice that was given to you by Roddy: forward declare as much as you can, but assume that some things must be included.
In both cases the compiler needs to know the size of the type. Therefore, a forward declaration will not suffice. A base class could add members or require a virtual table. The string member would require the size of the class to be increase to store the size of the STL string class.
Forward declaring STL classes is often inadvisable since the implementations commonly include explicit template instantiations that speed up compilation.
You're trying too hard to solve something that isn't actually a problem. Use the header files you need, and reduce - WHERE POSSIBLE - the requirement for them. But don't try and take it to extremes because you'll fail.
In some cases, the PIMPL idiom may help you, but not here.
For your base classes, you need to have the full type definition, not just a declaration. Derived type headers will need to #include the header for their base classes.
For classes in the std namespace, you must include the proper header - <string> in this case - and then do one of 3 things:
Fully qualify the type: std::string
aStringToTest
Put a using declaration for just
that type: using std::string;
Put in a using declaration for the
std namespace: using namespace std;
> Seems that forward declaration is useless for base classes and stl classes.
Correction...
Forward declaration is INAPPROPRIATE for base classes and object member. (It's not "useless", it is "inapplicable".)
A base class MUST be declared (not forward declared) when being declared as a based class of another class.
An object member MUST be declared (not forward declared) when being declared by another class, or as parameter, or as a return value. NOTE: by-reference or by-pointer does not have that constraint.
Correction...
Forward declaration of STL classes is -- as per ISO 14882 -- undefined behavior.
http://www.gotw.ca/gotw/034.htm