namespace Foo {
class Base {
[...]
};
};
In bar.c, I have a class method declared:
void doSomething(Foo::Base* foo);
I can #include foo.h and everything works fine, but I'd like to reduce
coupling by only including the headers I really need. All I should
really need to do is forward declare the class in bar.c, before the
above declaration:
class Foo::Base;
but that doesn't appear to be legal syntax. I'm stumped trying to
find a way to do this short of #including foo.h.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Try:
namespace Foo {
class Base;
}
-Gabe
Probably the namespace Foo is not visible here?
> class Foo::Base;
>
> but that doesn't appear to be legal syntax. I'm stumped trying to
> find a way to do this short of #including foo.h.
Regards,
Jyoti
namespace Foo {
class Base;
};
So far, so bad - the last semicolon (after the namespace) is wrong.
> In bar.c
A C file? I'll assume this is still C++ and also compiled as suche.
> I have a class method declared:
>
> void doSomething(Foo::Base* foo);
>
> [...] All I should really need to do is forward declare the
> class in bar.c, before the above declaration:
>
> class Foo::Base;
namespace Foo {
class Base;
}
Remember, you can reopen and extend namespaces, they are not bound to the
one definition rule the way that a type definition is.
Uli
> namespace Foo {
> class Base;
>
> }
>
> Remember, you can reopen and extend namespaces, they are not bound to the
> one definition rule the way that a type definition is.
Duh, of course. Thanks to the several people who pointed that out.
Now, let me ask the original, somewhat deeper question I really wanted
to ask. If I've got in my .c (Yes, C++) file, a method declared as:
void Operation(Foo::Base* b);
this is valid syntax for one of two situations:
1) There's a namespace Foo, with a member Base.
2) There's a class Foo, with an inner class Base.
Short of #including the full foo.h, is there any way I can insulate my
implementation code from knowing which of those two it is?
Right now, Foo is a namespace, but maybe at some time in the future I
want to change it to be a class. It would be nice (OK, I'm getting a
little theoretical here) not to have to touch the implementation files
which reference Foo::Base when I do that.
No. namespaces and classes are two very different beasts that can not
be used interchangeably.
Your code may not care that Base is an inner class or a namespace
member, but the compiler most certainly cares, because it changes the
rules that the compiler has to apply.
Bart v Ingen Schenau
Create a foo_fwd.h, which only declares a few types and is maintained
alongside with foo.h.
Note that other than a namespace, you can't reopen a class declaration. That
means that you can't declare a nested class without fully defining the
surrounding class.
Uli
--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932