简体   繁体   English

两个抽象类,派生自相同的基础 class。 如何访问从一个抽象 class 到另一个的指针

[英]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:我想知道的是:

  1. Is there a way I can access the pointer to the unit test class without giving function parameters?有没有办法在不提供 function 参数的情况下访问指向单元测试 class 的指针?

  2. Is there any way to not make this a static function?有什么办法不让它成为 static function?

  3. 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.

相关问题 从两个抽象类继承,其中一个还需要另一个基类的部分 - Inherit from two abstract classes, where one of them also needs parts of the other base class 从Abstract Base Class访问另一个Base Classes成员 - Access another Base Classes member from Abstract Base Class C ++在可以从派生类访问的抽象基类中存储类指针? - C++ Storing a class pointer in an abstract base class that can be accessed from derived classes? 从具体类派生抽象类 - Deriving an abstract class from concrete class 从具体类派生抽象模板类 - Deriving an abstract template class from concrete class 访问抽象类派生类的抽象类的向量! 怎么样? - Vector of an abstract class that access the derived classes of the abstract one ! How? 给定抽象基础 class X,如何创建另一个模板 class D<t> 其中 T 是从 X 派生的 class 的类型?</t> - given abstract base class X, how to create another template class D<T> where T is the type of the class deriving from X? class B 派生自抽象基础 class A,我如何在 ZA2F2ED4F8EBC2CBB4C21A2DZ A 中使用 singleton - class B derived from an abstract base class A, and how can i use singleton in class B? 从抽象基类指针到派生类的转换 - Casting from abstract base class pointer to derived class C ++多重继承,其基类派生自同一个类 - C++ multiple inheritance with base classes deriving from the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM