On 30/06/07, Shai Ayal <address@hidden> wrote:
> I want to declare a function returning the figure::figure_properties
> class.
Ok.
> This function will have to be declared before
> figure::figure_properties is declared.
Why? Oh, never mind. I'm sure there are good reasons to do this.
> This is usually done using forward declarations -- i.e. I would
> expect he following to work:
> class figure;
> class figure::figure_properties;
> figure::figure_properties test();
This won't work unless you have already given the full definition of
figure and you specified in that definition that figure_properties is
a subclass of figure.
C++ doesn't allow partial class definitions, which in a sense is what
you're attempting to do, and the reason is that otherwise the compiler
wouldn't know how much space to allocate for a class when declared if
later on you could potentially add more stuff to that class.
Since the only reason you might want nested classes, besides for a
comfortable syntax, is to grant the nested class access to the private
members of the enclosing class[1], what you can do is put both figure
and figure_properties in the same namespace and declare friendships in
order to give you the same access.
namespace fig{
class figure;
class properties;
}
// Later on...
class fig::figure{
// ...
friend class properties;
};
// Class definition must come after figure::base in order to have
// explicit friendship declared first by figure::base
class fig::properties{
// ...
};
Then you can return fig::properties from your
function. Unfortunately, now the base class figure is called
fig::figure, but you can typedef that away if you want to.
HTH,
- Jordi G. H.
[1] I think some versions of the C++ standard deny nested classes this
kind of access privilege, but the GNU implementation of C++ allows it.
At any rate, it will be allowed for C++09.