views:

53

answers:

3

I have a template class for which I need to access a protected member function of the template parameter, like this:

class Foo
{
protected:
    void foo() {}
};

template<typename T>
class Bar
{
public:
    static void bar(T& self){self.foo();}
};
...
Foo f;
Bar<Foo>::bar(f);

My problem is getting access to the protected method. I tried putting a friend class T into Bar, but that doesn't seem to be allowed in c++ (edit: and wouldn't solve my problem anyways, so it seemd). I tried letting Bar inherit from T (template<typename T> class Bar: public T (could have used private inheritance, but the public interface of Bar is not terribly important, since the class itself is internal only)), but that didn't allow for access of foo() either. So how do I get access to the foo() method?

Edit: Foo should not need to know Bar<Foo>, since there are quite a lot Bar classes. I can however make other changes to Foo (without changing the public interface of course).

A: 
template<typename T>
class Bar
{
public:
    static void bar(T& self){self.foo();}
};

class Foo
{
protected:
    void foo() {}
    friend class Bar<Foo>;
};

void main()
{
    Foo f;
    Bar<Foo>::bar(f);
}
Felics
While that would work it is not really the solution I'm looking for since I have several "Foo" and several "Bar" class (which can each be combined which each other), so putting in a friend class for each combination option is not really a good solution.
Grizzly
+1  A: 

You did your friend declaration in the wrong direction. If Bar says Foo is it's friend, that means Foo gets access to Bar's private data. For Bar to get access to Foo's private data, Foo has to say Bar is its friend.

Jerry Coffin
I thought it worked the other way arround, but then I normally avoid using friend, so anyway. As specified in my edit, putting Bar<Foo> as friend of Foo is not really an option, so while good to know, this doesn't really solve my problem (should have specfied sooner, I know)
Grizzly
It's either this or nothing. Read the answer I provided.
David Rodríguez - dribeas
+3  A: 

OK, this is a "rot in hell" hack. You can abuse the fact that you can form pointers-to-members pointing to protected base members from a derived class.

class Foo
{
protected:
    void foo() {}
};

// Helper template to bypass protected access control
// for a member function called foo, taking no parameters
// and returning void.
template<typename T>
struct Unprotect : public T
{
    typedef void (T::*FooPtr)();
    static FooPtr GetFooPtr()
    {
        return &Unprotect::foo;
    }
};

template<typename T>
class Bar
{
public:
    static void bar(T& self){(self.*Unprotect<Foo>::GetFooPtr())();}
};

int main()
{
    Foo f;
    Bar<Foo>::bar(f);
}
Charles Bailey
Hackish as this might be, it should do the job and since those classes are not for external use I can live with a bit ugly code, so thanks.
Grizzly
This is a lot of fun. There are also ways to circumvent private access: http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html :)
Johannes Schaub - litb
@litb: Nice, I too thought that it wasn't possible to subvert private protection.
Charles Bailey