+7  A: 

On the one hand, don't do that.

On the other hand, here's a gamble:

#define protected public
#include "foo.h"
#undef protected

8-)

But seriously, why is DoSomething() protected? Probably because calling it from external code can break something. In which case, you shouldn't be calling it from your tests.

RichieHindle
*Runs away, screaming*.
Dominic Rodger
Wouldn't friending be a better way to go here, if you absolutely have to test protected members (which I agree, you shouldn't be doing).
Dominic Rodger
@Dominic: Yes, but @sarah explicitly doesn't want to use `friend`.
RichieHindle
@RichieHindle - ah, thanks. Missed that.
Dominic Rodger
And this hack works (sometimes) when you have the header and a library, but not source, for the classes you want to exercise.
Don Wakefield
(to be fair, you do not recommend this, so this comment is not against your answer, but just against that technique). This probably works when "foo.h" is actually just a file with one class and nothing else, so you have complete control over this. But I would not do this, as anything that header includes indirectly will be affected too. Probably a whole other piece of code. It could cause a different ABI interface which can cause strange effects - you never know what the compiler does internally - maybe it orders member by their accessibility, or something.
Johannes Schaub - litb
@RichieHindle - I'm going to reassess whether the function needs to be protected in the first place. Thanks for the advice :)
sarah
+1  A: 

If it is strictly test code, you could do...

#define protected public
#include "Foo.h"

// test code

#undef protected
Evän Vrooksövich
This gives me nightmares
Charles Salvia
Me too actually.
Evän Vrooksövich
Scary stuff - but it would work...
Dror Helper
+2  A: 

I've done

class Foo
{
protected:
    void DoSomething(Data data);
public:
    #ifdef TEST
    void testDoSomething(Data data);
    #endif
}

Then compile your unit tests with g++ -D TEST.

Dan Hook
+3  A: 

Rather than ifdefing private to public, consider ifdefing friendship, or better yet think if that function really needs to belong to that class, maybe it would suffice to have something in a named/unnamed namespace in a cpp, and then declared in a test project.

Anyway, check this link, maybe your testing framework would provide similar functionality.

EDIT: Did you consider inheriting your test class from your real class?

Dmitry
+6  A: 

Ok, since you said it is only a test code I am going to suggest something seriously hacky but would work:

struct tc : protected Foo
{
    tc(Foo *foo, Data& data)
    {
        ((tc*)foo)->DoSomething(data);
    }
};

Blah blah;
tc t(&blah.foo, blah.data);
Murali VP
Thanks for all the great suggestions. I decided to go with this solution for now.
sarah
+2  A: 

You could use inheritance with forwarding functions:

class Foo
{
protected:
    void DoSomething(Data data);
}

class test_Foo : public Foo
{
public:
    void testDoSomething(Data data)
    {
        DoSomething(data);
    }
}
mocj
You'd probably want to combine this with Dependency Injection so you can replace the Foo in Blah with a test_Foo instance.
Mark Bessey
+2  A: 

Use wrapper as follows:

// Foo.h unchanged

// Blah.h unchanged

// test code
class FooTest : public Foo { friend void test(); }; // make friends

void test()
{
    Blah blah;
    static_cast<FooTest*>(&blah.foo)->DoSomething(blah.data); // Here's no problem!    
}
Kirill V. Lyadvinsky
+3  A: 

There is a way which is completely allowed by the Standard.

//in Foo.h 
class Foo
{
protected:
    void DoSomething(Data data);
};

//in Blah.h
class Blah
{
public:
    Foo foo;
    Data data; 
};

//in test code...
struct FooExposer : Foo {
  using Foo::DoSomething;
};

Blah blah;
(blah.foo.*&FooExposer::DoSomething)(blah.data);

Read the Hidden features of C++ entry for an explanation.


You may write a macro for your convenience (the parenthesis are there so that you can use this macro also for types that have a comma, like vector<pair<A, B>>):

#define ACCESS(A, M, N) struct N : get_a1<void A>::type { using get_a1<void A>::type::M; }

template<typename T> struct get_a1;
template<typename R, typename A1> struct get_a1<R(A1)> { typedef A1 type; };

The matter now becomes

ACCESS((Foo), DoSomething, GetDoSomething);
Blah blah;
(blah.foo.*&GetDoSomething::DoSomething)(blah.data);
Johannes Schaub - litb
Fine indeed. A pity it would not work with `private` methods.
Matthieu M.