N
Niklas Norrthon
I want to share a technique I recently have found to be useful
to get around some obstacles that data protection can raise.
Consider the following class:
// foo.h
#ifndef H_FOO
#define H_FOO
class Foo
{
private:
struct ImplementationDetails
{
int x;
int f() const;
} imp;
/* more data members */
public:
/* public interface */
Foo();
void mem_fun();
/* etc. */
/* friends */
friend void friend_fun(ImplementaionDetails&);
};
#endif
The function friend_fun is not a member of the class, but
can be considered as a part of the public interface. Typical
real life examples of this are operator<<(ostream&, const T&),
and operator+ (const T&, const T&) for a user defined type T.
A friend of a class has by definition access to that class's
private members, but it pass on the access rights to
other external functions. Usually this is not a problem, since
it can retrieve the data needed from the class, and pass along
that data. But when it comes to types it's a different matter.
Types are not values so they can't be passed as arguments in
the normal sence. Which means data that is of private declared
types can't be passed to any external functions.
For example:
// foo.cc (first try)
#include "foo.h"
namespace {
void helper(Foo::ImplementationDetails& i) /* problem */
{
/* do something */
i.f(); /* f is public, no problem */
}
}
void friend_fun(Foo::ImplementationDetails& imp)
{
helper(imp);
}
This does not work, because helper is not a friend of Foo. And
since helper is just a way to implement friend_fun, and not a public
function that should be called from anyone else it shouldn't be
made friend either.
One way to get around this is with a struct local to the friend
class, with static member functions:
// foo.cc (second try)
#include "foo.h"
void friend_fun(Foo::ImplementationDetails& imp)
{
struct Local
{
static void helper(Foo::ImplementationDetails& i) /* this works! */
{
/* do something */
i.f(); /* f is public, no problem */
}
};
Local::helper(imp);
}
There are two problems with this approach:
1. The implementation of Local::helper() must be inline, which makes
the code more difficult to follow (IMHO).
2. If helper common to two or more friends its code must be repeated
in every such function, which really is asking for trouble.
Now finally the solution is of course to use templates, as I have
already hinted in the subject line:
// foo.cc (third and final version */
#include "foo.h"
namespace {
template <typename Foo_ImpDet>
void helper(Foo_ImpDet& i)
{
/* do something */
i.f(); /* works for all types of i with a public f() member */
}
}
void friend_fun(Foo::ImplementationDetails& imp) /* unchanged from first try */
{
helper(imp);
}
I'm not asking any questions, just wanted to share. Comments are welcome.
/Niklas Norrthon
to get around some obstacles that data protection can raise.
Consider the following class:
// foo.h
#ifndef H_FOO
#define H_FOO
class Foo
{
private:
struct ImplementationDetails
{
int x;
int f() const;
} imp;
/* more data members */
public:
/* public interface */
Foo();
void mem_fun();
/* etc. */
/* friends */
friend void friend_fun(ImplementaionDetails&);
};
#endif
The function friend_fun is not a member of the class, but
can be considered as a part of the public interface. Typical
real life examples of this are operator<<(ostream&, const T&),
and operator+ (const T&, const T&) for a user defined type T.
A friend of a class has by definition access to that class's
private members, but it pass on the access rights to
other external functions. Usually this is not a problem, since
it can retrieve the data needed from the class, and pass along
that data. But when it comes to types it's a different matter.
Types are not values so they can't be passed as arguments in
the normal sence. Which means data that is of private declared
types can't be passed to any external functions.
For example:
// foo.cc (first try)
#include "foo.h"
namespace {
void helper(Foo::ImplementationDetails& i) /* problem */
{
/* do something */
i.f(); /* f is public, no problem */
}
}
void friend_fun(Foo::ImplementationDetails& imp)
{
helper(imp);
}
This does not work, because helper is not a friend of Foo. And
since helper is just a way to implement friend_fun, and not a public
function that should be called from anyone else it shouldn't be
made friend either.
One way to get around this is with a struct local to the friend
class, with static member functions:
// foo.cc (second try)
#include "foo.h"
void friend_fun(Foo::ImplementationDetails& imp)
{
struct Local
{
static void helper(Foo::ImplementationDetails& i) /* this works! */
{
/* do something */
i.f(); /* f is public, no problem */
}
};
Local::helper(imp);
}
There are two problems with this approach:
1. The implementation of Local::helper() must be inline, which makes
the code more difficult to follow (IMHO).
2. If helper common to two or more friends its code must be repeated
in every such function, which really is asking for trouble.
Now finally the solution is of course to use templates, as I have
already hinted in the subject line:
// foo.cc (third and final version */
#include "foo.h"
namespace {
template <typename Foo_ImpDet>
void helper(Foo_ImpDet& i)
{
/* do something */
i.f(); /* works for all types of i with a public f() member */
}
}
void friend_fun(Foo::ImplementationDetails& imp) /* unchanged from first try */
{
helper(imp);
}
I'm not asking any questions, just wanted to share. Comments are welcome.
/Niklas Norrthon