472,973 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 software developers and data experts.

Interesting technique using templates to get around privacy


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
Dec 9 '05 #1
2 1435
Niklas Norrthon wrote:
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.


I have one question which, when you answer it, can lead to more questions,
of course.

Who and how uses 'friend_fun' function? To call it one would need to
expose 'imp' member, and that's already implicitly allowing access to
private parts of Foo. I would be surprised if you could do the same
thing if 'friend_fun' would have 'Foo' as its argument. Or maybe not.
Try changing it to

void friend_fun(Foo&);

and see what needs to be changed in your template technique (if anything
at all). I am just curious.

V
Dec 9 '05 #2
Victor Bazarov <v.********@comAcast.net> writes:
Niklas Norrthon wrote:

// 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.


I have one question which, when you answer it, can lead to more questions,
of course.

Who and how uses 'friend_fun' function? To call it one would need to
expose 'imp' member, and that's already implicitly allowing access to
private parts of Foo. I would be surprised if you could do the same
thing if 'friend_fun' would have 'Foo' as its argument. Or maybe not.
Try changing it to

void friend_fun(Foo&);

and see what needs to be changed in your template technique (if anything
at all). I am just curious.


You are right. I meant to have the friend_fun to take an argument of Foo&
and not Foo::ImplementationDetails&. I was typing too fast. The fourth
and (hopfully) final version of the above:

// 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(Foo&); };
#endif

// foo.cc (fourth 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& foo)
{
helper(foo.imp);
}

As I mentioned in my first post, a concrete example of when this technique
could be useful is a class for which operator<< is defined:

class Qwerty
{
public:
Qwerty();
virtual ~Qwerty() { }
/* etc */
friend std::ostream& operator<< (ostream&, const Qwerty&);

private:
typedef std::vector<std::string> Vec;
typedef Vec::const_iterator Iter;

Vec my_data;
};

template <typename Iter>
bool is_visible(Iter i)
{
/* complex logic to find out if *i should be printed or not */
return result_of_logic_above;
}

std::ostream& operator<< (ostream& os, const Qwerty& q)
{
for (Qwerty::Iter i = q.my_data.begin(); i != q.my_data.end(); ++i)
{
if (is_visible(i)) {
os << *i << ' ';
}
}
return os;
}

/Niklas Norrthon
Dec 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Torsten Curdt | last post by:
template<class TYPE, class ELEMENT> class MyClass : public TYPE<ELEMENT,ELEMENT> { }; This does not seem to work - but why? -- Torsten
3
by: Fran Cotton | last post by:
Hi, I'd greatly appreciate it if someone could cast light on my problem - I can't seem to find any reference to it anywhere. Consider the following XML: <paragraph> I am...
3
by: Sandros | last post by:
Background: I'm collecting usability statistics for a group of applications. Each count has the following attributes: date, application, major heading, minor heading, count. My intent is to pull...
0
by: Audrey Pratt | last post by:
Happy Holidays to you and allow us to play Santa this year with these awsome deals that in anyway you can refuse: 2000 Web Templates for only $18.00 (Savings Over $1,000.00) ...
2
by: Scamjunk | last post by:
I have been desperately looking for a treeview-type solution for my problem for the past three weeks and have been greatly unsuccessful. I am totally new to the world of XSLT and I *don't know*...
2
by: IR | last post by:
Hi, Still playing around with templates, I have the following questions... I know how to express "positive" compile time assertions of a class's interface in templates (ie. T has-a member...
2
by: shapper | last post by:
Hello, I am for days trying to apply a XSL transformation to a XML file and display the result in a the browser. I am using Asp.Net 2.0. Please, could someone post just a simple code example,...
8
by: sagi.perel | last post by:
I have tried to compile the following code on Win & Unix. Doesn't work on either. <----- CODE -----> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <vector> #include...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.