Is it really impossible to pre-declare a class inside a pre-declaredclass?

J

Juha Nieminen

If we want to pre-declare a class inside a namespace, that's easy:

namespace A { class B; }

void foo(const A::B*); // ok

However, assume that A is a class instead of a namespace. It would be
really nice if one could do something like:

class A;
class A::B; // error

void foo(const A::B*); // nope

Being able to do like this would reduce #include dependencies (which
in some cases are rather annoying).

Maybe there's another way of doing this I have missed? (Taking B out
of A is not a good option, as the whole purpose of it being inside A is
so that it doesn't garbage the outer namespace.)

It would also be nice if other types inside incomplete classes could
also be pre-declared, to reduce #include dependencies. For example:

class A;
enum A::SomeType;

void foo(A::SomeEnum type);

Or something similar.
 
M

Marcel Müller

Juha said:
If we want to pre-declare a class inside a namespace, that's easy:

namespace A { class B; }

void foo(const A::B*); // ok

However, assume that A is a class instead of a namespace. It would be
really nice if one could do something like:

class A;
class A::B; // error

void foo(const A::B*); // nope

As far as I know: no way!

The only thing you can do is to predeclare a nested object inside the
class A.

Being able to do like this would reduce #include dependencies (which
in some cases are rather annoying).

Well, the best way to reduce dependencies is to use abstract interface
classes and private implementation classes which are not seen from
outside. The drawback is that you lose the inline optimizations in case
of trivial functions and if your class is not polymorphic anyway you get
additional virtual function calls at runtime. They are expesive on many
platforms.
The less the compiler knows the less dependencies you have and the less
it can optimize.

Maybe there's another way of doing this I have missed? (Taking B out
of A is not a good option, as the whole purpose of it being inside A is
so that it doesn't garbage the outer namespace.)

What about making another nested namesspace inside the one where A is
declared. Maybe that would be a good place for B.

It would also be nice if other types inside incomplete classes could
also be pre-declared, to reduce #include dependencies. For example:

Same problem, no solution.


Btw.: I also wish things like that to work smoother and that namespaces
and classes get closer together. In case of static or declarative
objects like static members or nested types these should be straight
forward. You might want import the names of a classes object with "using
namesapace" or something like "using class A" in a certain part of code.


Marcel
 
D

Daniel T.

Juha Nieminen said:
If we want to pre-declare a class inside a namespace, that's easy:

namespace A { class B; }

void foo(const A::B*); // ok

However, assume that A is a class instead of a namespace.

Then do it.

class A { class B; };

I do this all the time. See (http://www.gotw.ca/gotw/024.htm) for an
example of why it is useful.
 
J

Juha Nieminen

Daniel said:
class A { class B; };

That obviously doesn't work because now A is a whole definition, not a
pre-definition. (And besides, A::B is private and thus not accessible to
the foo() function.)

When the real class A is later defined, a redefinition error obviously
happens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top