On Thu, Mar 13, 2003 at 01:02:35AM +0200, Tuomo Valkonen wrote:
> Is it somehow possible, without doing it individually for all those
> functions, to define non-class functions in a file "friends" for a
> class so that protections don't apply? I'd hate to have to introduce
> every minor helper function in the class body. If it is not possible,
> then I'd have to use no protections at all if I ever decide to use
> c++ classes.
I suspect what you want is something like the "pimpl" idiom as
outlined in Sutter's "Exceptional C++". It's not the nicest name but
it works out quite nice (pimpl stands for "private implementation").
In the .h file you place (usual syntax disclaimers apply):
class Foo {
public:
// The public interface
Foo();
~Foo();
private:
struct FooImpl* m_impl;
};
In the .cc file:
struct Foo::FooImpl {
// All the helper functions, data, etc. you want
};
Foo::Foo()
: m_impl(new Foo::FooImpl())
{
// ...
}
Foo::~Foo() {
// ...
delete m_impl;
}
This way you can change all you want in the body of FooImpl, without
having to change the Foo interface or the .h file at all. This is
usually used to avoid having to change a .h file with a lot of reverse
dependencies whenever the implementation of a class changes. You may
want to put a pointer back to the Foo object inside of the FooImpl
class, but it's not always necessary.
For more information, see Herb Sutter's "Exceptional C++"
(Addison-Wesley Longman, 2000). It's good some good tricks like the
above and is very readable.
--
Stefanus Du Toit; http://3.141592.org/; gpg 4bf2e217; #include <iostream>
template<int i,int j=i-1>struct I{I(){if(i%j)I<i,j-1>();else I<i-1>();}};
template<int i>struct I<i,1>{I(){std::cout<<i<<'\n';I<i-1>();}};template<
>struct I<1,0>{I(){}};int main(){I<50>();}/* Use -ftemplate-depth-5000 */