Reputation: 4251
Let's say I have a C++ class template:
template <class _T>
class MyClass
{
public:
int func();
private:
_T internal;
};
I'd like a way to specify a boolean to this template that, when true, will make every member in this template public.
For example:
MyClass<SomeClass, false> c1;
c1.internal.someFunc(); // ERROR
MyClass<SomeOtherClass, true> c2;
c2.internal.someFunc(); // SUCCESS
For those wondering, I'm using gtest and gmock to mock up concrete classes. So, in one of my unit tests I will have something like:
TEST(MyClass, Test1) {
MyClass<SomeMockClass, true> c1;
EXPECT_CALL(c1.internal, someFunc()).Times(1);
}
For this test template, internal must be accessible by my code. In production, I'd like to hide that from the user.
I'm using msvc 11 (Visual Studio 2012), so I have access to some C++11 features and metaprogramming constructs.
Upvotes: 3
Views: 308
Reputation: 1699
You can inherit a test helper from the main class, helper that you declare friend, and then promote the elements that you are interested in public:
#include <iostream>
template<typename T> class TestMyClass;
template<typename T>
class MyClass
{
friend class TestMyClass<T>;
public:
int func();
private:
T internal;
};
template <class T>
class TestMyClass : public MyClass<T>
{
public:
using MyClass<T>::internal;
};
int main()
{
TestMyClass<double> s;
s.internal = 5;
std::cout << s.internal << "\n";
return 0;
}
Upvotes: 4
Reputation: 26080
You can do this with class template specialization, however, it does mean you need to modify both sets of code when you make a change:
#include <iostream>
template <class T, bool visible>
class MyClass
{
};
template <class T>
class MyClass<T, true>
{
public:
int func();
T internal;
};
template <class T>
class MyClass<T, false>
{
public:
int func();
private:
T internal;
};
int main()
{
MyClass<int, true> s;
s.internal = 5;
std::cout << s.internal << "\n";
return 0;
}
Upvotes: 1