比方說,我有一個C++類模板:更改類模板成員可見
template <class _T>
class MyClass
{
public:
int func();
private:
_T internal;
};
我想一個方法來指定一個布爾值,該模板,當屬實,將會使每一個成員在此模板公衆。
例如:
MyClass<SomeClass, false> c1;
c1.internal.someFunc(); // ERROR
MyClass<SomeOtherClass, true> c2;
c2.internal.someFunc(); // SUCCESS
對於那些想知道,我使用GTEST和gmock來模仿具體類。所以,在我的單元測試一個,我會碰到這樣的:
TEST(MyClass, Test1) {
MyClass<SomeMockClass, true> c1;
EXPECT_CALL(c1.internal, someFunc()).Times(1);
}
對於這個測試模板,內部必須是我的代碼訪問。在製作過程中,我想將其隱藏起來。
我使用msvc 11(Visual Studio 2012),所以我可以訪問一些C++ 11功能和元編程結構。
'_T'絕對不是一個好用的名字。它被保留用於實現,特別是這一個,因爲我知道它正在用作一個宏來根據您的程序是否使用寬字符的東西來擴展字符串。 – chris
好貼士@chris。我以此爲例,在模板中我幾乎更具體,但這很好理解。 – jmacdonagh
如果你有興趣,http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – chris