How to Forward Declare a Class Which Is in a Namespace

How to forward declare a class which is in a namespace

To forward declare class type a in a namespace ns1:

namespace ns1
{
class a;
}

To forward declare a type in multiple level of namespaces:

namespace ns1
{
namespace ns2
{
//....
namespace nsN
{
class a;
}
//....
}
}

Your are using a a member of consumer which means it needs concrete type, your forward declaration won't work for this case.

Why can't I forward-declare a class in a namespace using double colons?

Because you can't. In C++ language fully-qualified names are only used to refer to existing (i.e. previously declared) entities. They can't be used to introduce new entities.

And you are in fact "reopening" the namespace to declare new entities. If the class Class is later defined as a member of different namespace - it is a completely different class that has nothing to do with the one you declared here.

Once you get to the point of defining the pre-declared class, you don't need to "reopen" the namespace again. You can define it in the global namespace (or any namespace enclosing your Namespace) as

class Namespace::Class {
/* whatever */
};

Since you are referring to an entity that has already been declared in namespace Namespace, you can use qualified name Namespace::Class.

Forward declaration of a namespaced C++ class in in Objective C

You just declare it as usual, and wrap it in the C++ #ifdef check when the header is also included in C and/or ObjC translations:

#ifdef __cplusplus
namespace name {
class Clazz;
} // << name
#endif

For obvious reasons, you cannot use name::Clazz in a C or ObjC translation, so this works out perfectly. You either need the forward declaration in the C++ translation, or it does not need to be visible to C or ObjC.

forward declaration of namespace

Nothing says a namespace needs all of its contents right away:

namespace NS {}
namespace CounterNameSpace {
int upperbound;
int lowerbound;
using namespace NS;
}
namespace NS {
int i;
}

However, this might not do what you want. You still won't be able to use any of the types in that namespace until you've declared them.

Forward declaring classes in namespaces

Seems as though the answer lies in the C++ specification:

3.3.5 "Namespace scope" in the standard.

Entities declared in a namespace-body
are said to be members of the
namespace, and names introduced by
these declarations into the
declarative region of the namespace
are said to be member names of the
namespace.

A namespace member can also be
referred to after the :: scope
resolution operator (5.1) applied to
the name of its namespace or the name
of a namespace which nominates the
member’s namespace in a
using-directive;

C++ namespaces - forward declaration and visibility of nested namespaces

Your declaration of LibClass is wrong. It should be:

namespace lib {
class LibClass;
}

class lib::LibClass {
...
};

or

namespace lib {
class LibClass {
....
};
}

As it currently stands, you are declaring lib::LibClass and defining ::LibClass (two entirely different classes).

This applies to all your other classes too.

Should one forward declare classes from a namespaced library?

As @molbdnilo pointed out, there is nothing wrong with forward declaring with namespace. First option is not an option at all, for various reasons I dont want to include header until I have to, forward declaration is always preferred way.
Why dont you just provide a header with forward declarations as many boost implementations do? for example boost spirit numerics_fwd.hpp?

Ah, missed @DevSolar comment. IMHO this is the best solution.

How to forward declare class which is in unnamed namespace

My question is how to declare class which will be defined in unnamed namespace in .cpp file

You cannot. The unnamed namespace is explicitly meant to be privately visible for the current translation unit it appears in, and cannot be used for forward declarations inherently.

You're probably be better off using the pimpl idiom, if you want to hide implementation details.


Another popular approach is using an internal_ namespace, and document it's not meant for public usage:

namespace calculators {
namespace internal_ {
struct PrevCalc{
double prevA = -1;
double prevB = -1;
double prevC = -1;
};
}

class Calculator {
public:
Calculator();
private: // !!!!
internal_::PrevCalc* prevCalc;
};
}


Related Topics



Leave a reply



Submit