[英]Two abstract classes, deriving from the same base class. How can I access a pointer from one abstract class to the other
I'm writing a helper class for my cppUnit test harness to among other things print test descriptions with spaces.我正在为我的 cppUnit 测试工具编写一个助手 class 以打印带有空格的测试描述。 Since various elements are concatenated by the test harness to name the test class, spaces in the test name cause errors.
由于测试工具连接了各种元素以命名测试 class,因此测试名称中的空格会导致错误。 It may sound trivial, but when you are looking through test output of 60 tests which are all, similarly named run on sentences in camel case, spaces are nice.
这听起来可能微不足道,但是当您查看测试 output 的 60 个测试时,这些测试都类似地命名为 run on sentence in camel case,空格很好。
Since I don't want to touch the harness, I created my helper class so that it derives from the class Test, each individual test is also a class derived from test.因为我不想碰线束,所以我创建了我的助手 class 以便它派生自 class 测试,每个单独的测试也是从测试派生的 class。 I'm pretty bad at polymorphism, so be patient with me.
我不擅长多态性,所以请耐心等待。 What I am trying to do is access from my helper class, the member getName(), in the unit test class which is a public member inherited from the Test base class.
我要做的是从我的助手 class 成员 getName() 访问单元测试 class ,这是从测试库 class 继承的公共成员。
Here is an excerpt of what I have done so far.这是我迄今为止所做的摘录。 It does work, however I want something more clean in the actual calling of this function.
它确实有效,但是我希望在实际调用这个 function 时更干净一些。
TestHelperClass测试助手类
class TestHelperClass : public Test
{
public:
virtual ~TestHelperClass(){}
template <typename T>
static const char* getReadableName(T testClassPtr)
{
char * uglyName;
int ugLen;
ugLen = strlen(testClassPtr->getName().asCharString());
uglyName = (char*)malloc(ugLen);
memcpy(uglyName, testClassPtr->getName().asCharString(), ugLen);
std::string prettyName;
// Handle conversion...
free(uglyName);
return prettyName.c_str();
}
};
Unit test单元测试
FYI this is a macro taking two arguments a group and a name, and generating a class which inherits the Test class.仅供参考,这是一个宏,采用两个 arguments 一个组和一个名称,并生成一个继承测试 class 的 class。
TEST(groupNameString, reallyLongTestDescriptionString)
{
printf("%s\n", TestHelperClass::getReadableName(this));
// remainder of the unit test.
}
Obviously, I'm far from incomplete.显然,我远非不完整。 What I want to know is:
我想知道的是:
Is there a way I can access the pointer to the unit test class without giving function parameters?有没有办法在不提供 function 参数的情况下访问指向单元测试 class 的指针?
Is there any way to not make this a static function?有什么办法不让它成为 static function?
If the answer is no to one or both of the above, is there a way to use macro's to create a more simple interface for the user, writing the test?如果上述一个或两个答案是否定的,有没有办法使用宏为用户创建一个更简单的界面,编写测试?
You can make the getReadableName function part of the Test class.您可以将 getReadableName function 作为测试 class 的一部分。 Then, your derived class can access it directly.
然后,您派生的 class 可以直接访问它。
Test(groupNameString, reallyLongTestDescriptionString)
{
printf("%s\n", this->getReadableName());
// remainder of the unit test.
}
You can use the name given in the macro to set a protected variable inside Test class that will be converted in your getReadbleName.您可以使用宏中给出的名称在 Test class 中设置一个受保护的变量,该变量将在您的 getReadbleName 中进行转换。 You would not need a template, btw.
顺便说一句,您不需要模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.