[C++] Can I forward declare nested classes?

Started by
10 comments, last by MaulingMonkey 18 years, 10 months ago
Nah MaulingMonkey, I've just been having that same problem as the OP, and felt like shoving my nose into another's thread [grin]
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Advertisement
Ahh okay ^_^.

I have a huge grudge against nested classes and don't use them*. I instead prefer to use typedefs to achieve my desired effect. For example, if I were implementing my own version of std::vector...:

namespace std { //you should almost never ever do this.    template < typename value_t >    class vector;    template < typename value_t >    class _Impl_vector_iterator {        friend class vector< value_type >;    public:        typedef value_t value_type;    private:        value_type * data ;        _Impl_vector_iterator ( value_type * data )            : data( data )        {        }    public:        bool operator==( const _Impl_vector_iterator & other ) const {            return data == other.data;        }        //...    };    template < typename value_t >    class vector {    public:        typedef std::size_t size_type;        typedef value_t value_type;        typedef _Impl_vector_iterator< value_type > iterator;        typedef _Impl_vector_iterator< const_value_type > const_iterator;    private:        size_type m_size; //I don't like pimpls for the most part, but...        value_type * data;    public:        size_type size( void ) const { //...people should use this function...            return m_size; //...so this is one of the two or three uses absolutely needed.        }        iterator begin( void ) {            return iterator( data ); //note: uses the private ctor, which is why we're a friend.        }        iterator end( void ) {            return iterator( data + size() ); //note: uses the private ctor again        }        const_iterator begin( void ) const {            return const_iterator( data );        }        const_iterator end( void ) const {            return const_iterator( data + size() ); //note: we use size() instead of m_size whenever possible.        }        //...    };}


Note that this source is very (very) incomplete,

* with the very occasional exception, namely with Boost.Spirit which requires this syntax to define grammars, and similar cases of template-fu.

This topic is closed to new replies.

Advertisement