Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Forward reference to a class in a namespace?

2 views
Skip to first unread message

Roy Smith

unread,
Jan 4, 2010, 9:31:32 PM1/4/10
to
I've got a header, foo:h, which has:

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! ]

GRedner

unread,
Jan 5, 2010, 12:49:46 AM1/5/10
to
On Jan 4, 9:31 pm, Roy Smith <r...@panix.com> wrote:
> 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.

Try:

namespace Foo {
class Base;
}

-Gabe

Jyoti Sharma

unread,
Jan 5, 2010, 12:49:39 AM1/5/10
to

"Roy Smith" <r...@panix.com> wrote in message
news:a2973239-1d8a-47aa...@e37g2000yqn.googlegroups.com...

> I've got a header, foo:h, which has:
>
> 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:
>

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

red floyd

unread,
Jan 5, 2010, 12:49:31 AM1/5/10
to
On 1/4/2010 6:31 PM, Roy Smith wrote:
> I've got a header, foo:h, which has:
>
> 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.

namespace Foo {
class Base;
};

Ulrich Eckhardt

unread,
Jan 5, 2010, 12:50:44 AM1/5/10
to
Roy Smith wrote:
> I've got a header, foo:h, which has:
>
> 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

Roy Smith

unread,
Jan 5, 2010, 2:18:34 PM1/5/10
to
On Jan 5, 12:50 am, Ulrich Eckhardt <dooms...@knuut.de> wrote:

> 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.

Bart van Ingen Schenau

unread,
Jan 6, 2010, 1:08:32 PM1/6/10
to
On Jan 5, 8:18 pm, Roy Smith <r...@panix.com> wrote:
> On Jan 5, 12:50 am, Ulrich Eckhardt <dooms...@knuut.de> wrote:
>
> > 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?

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

Ulrich Eckhardt

unread,
Jan 11, 2010, 5:07:49 PM1/11/10
to
Roy Smith wrote:
> 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?

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

0 new messages