Topic: Forward declaration of class templates?
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/03/18 Raw View
Daniel Parker wrote:
[...]
> You could write the forward reference as
>
> namespace std {
> template<class T> class allocator;
> template <class T,class A=allocator<double> > class vector;
^^^^^^^^ really??? Not <T>?
[...]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Daniel Parker" <danielp@no_spam.com>
Date: 1999/03/18 Raw View
Christopher Eltschka <celtschk@physik.tu-muenchen.de> wrote in article
<36F0D8C4.2666274D@physik.tu-muenchen.de>...
>
> Daniel Parker wrote (but wished he hadn't):
>
> > namespace std {
> > template<class T> class allocator;
> > template <class T,class A=allocator<double> > class vector;
> ^^^^^^^^ really??? Not <T>?
>
Uh, sorrry about that.
Regards,
Daniel Parker danielp@no_spam.anabasis.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Daniel Parker" <danielp@no_spam.com>
Date: 1999/03/18 Raw View
Jarl Christian Berentsen <chrisb@funcom.com> wrote in article
<36EF985D.5184198C@funcom.com>...
>
> In order to avoid too much dependencies in headerfiles I want to do
> something like:
>
> // forward declarating classes
> class std::vector<T>; // does not work
> class Fool;
>
> class Foo
> {
> Fool* m_pcFool;
> std::vector<Fool>* m_pcFoolVector;
> };
>
You could write the forward reference as
namespace std {
template<class T> class allocator;
template <class T,class A=allocator<double> > class vector;
};
and declare Foo as
class Fool;
class Foo
{
std::vector<Fool>* m_pcFoolVector;
};
If you do this, make sure that you put the forward references in a separate
header file, say stlfwd.h, rather than duplicating them throughout your
code, since it may be necessary to modify them if the standard should
change with respect to the number of template arguments in vector. It
would be far preferable if the standard library had thought to provide such
a header file itself, like it does with iosfwd.h, but unfortunately that's
not the case. In general, you should do the same thing with your forward
references, collect them in separate header files, don't let them
proliforate. Who knows, you might someday want to redefine Fool in terms
of something else using a typedef, and you might need to change all _your_
forward references..
However, you don't need to forward declare the standard library's vector
class to achieve your goal of eliminating dependencies in header files.
Just use the pimpl idiom, e.g.
/////////////////
// Foo.h
// pull in your own forward references including Fool
#include "MyLibFwd.h"
class Foo_Abstract
{
public:
virtual Fool* at( int i ) = 0;
};
class Foo
{
public:
Foo();
Fool* at( int i )
{
return d_pImpl->at( i );
}
private:
Foo_Abstract* d_pImpl;
};
////////////////////
// Foo.cpp
#include <vector>
#include "Fool.h"
class Foo_Impl : public Foo_Abstract
{
public:
virtual Fool* at( int i );
private:
std::vector d_fools;
};
Foo::Foo()
{
d_pImpl = new Foo_Impl;
}
Fool* Foo_Impl::at( int i )
{
// Implemented in terms of vector
}
And check out John Lakos' book Large-Scale C++ Software Design for
excellent advise on this and related matters.
Regards,
Daniel Parker danielp@no_spam.anabasis.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]