I'm trying to use a type alias of an object in another header without including header file .
My simplified version of code is :
// A.h
#include <vector>
using Vector=std::vector<int>;
====================================================
//B.h
using Vector;//forward declaration but not working !(Vector has not beed declared)
int foo(Vector*);
====================================================
//B.cpp
#include "A.h"
void foo(Vector*){}
I don't want to write using Vector=std::vector<int>;
again in B.h
because It's definition must be same as definition of Vector
in A.h
and It may change in future and I can't include it because my code have circular dependency.
Is forward declaration of using
possible in c++11 ?
In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this. Example: // Forward Declaration class A class A; // Definition of class A class A{ // Body };
To write a forward declaration for a function, we use a function declaration statement (also called a function prototype). The function declaration consists of the function header (the function's return type, name, and parameter types), terminated with a semicolon. The function body is not included in the declaration.
In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.
Generally you would include forward declarations in a header file and then include that header file in the same way that iostream is included.
It is not possible, however the following could be a workaround.
// Common.h
...
#include <vector>
using Vector=std::vector<int>;
...
Afterwards you include Common.h
in both, A.h
and B.h
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With