473,325 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 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 1448
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.