继承成员函数指针

时间:2016-03-10 05:39:49

标签: c++ pointers c++11 function-pointers member-function-pointers

我在网上看到了这个代码,我想知道它是如何实现的。由于成员函数指针不能分配给基类的成员函数指针,我很好奇存储派生类的成员函数的指针以及如何存储。

这是包含测试声明的头文件

#ifndef TestStudent_h
#define TestStudent_h

#include <iostream>
#include <string>

// Note 1
#include "TestCase.h"
#include "TestSuite.h"
#include "TestCaller.h"
#include "TestRunner.h"

#include "Student.h"

class StudentTestCase : public TestCase { // Note 2 
public:
  // constructor - Note 3
  StudentTestCase(std::string name) : TestCase(name) {}

  // method to test the constructor
  void testConstructor();

  // method to test the assigning and retrieval of grades
  void testAssignAndRetrieveGrades();

  // method to create a suite of tests
  static Test *suite ();
};
#endif

这是将成员函数添加到某种列表

的函数的实现
// method to create a suite of tests - Note 7
Test *StudentTestCase::suite () {
  TestSuite *testSuite = new TestSuite ("StudentTestCase");

  // add the tests
  testSuite->addTest (new TestCaller  
      ("testConstructor", &StudentTestCase::testConstructor));
  testSuite->addTest (new TestCaller  
      ("testAssignAndRetrieveGrades", 
       &StudentTestCase::testAssignAndRetrieveGrades));
  return testSuite;
}

我想知道成员函数存储的数据类型是什么,因为它们不能存储在基类已知的任何函数指针类型中。而且它们存储的位置必须知道类的类型这些都是定义的,因为任何调用这些对象的实体都需要&#34; link&#34;这些函数与那种类型的对象太对了?具体来说,在这个函数中TestCaller如何知道如何调用添加到它的成员函数?

1 个答案:

答案 0 :(得分:4)

我的猜测是TestCaller有一个看起来像

的构造函数
template<class Callee>
TestCaller(
    const std::string &description, 
    void (Callee::*test_method)());

请注意:

  1. 在此构造函数的主体内(即,在实例化时),Callee的类型已知。

  2. TestCaller本身必须以不知道&#34;知道&#34;的方式存储test_method Callee,因为它本身不是Callee参数化的模板类(事实上,可能只有一个Callee)。

  3. 所以这是类型擦除的经典案例。有许多库可以执行此操作(例如,boost::TypeErasureboost::any)。

    这个想法是TestCaller存储(可能是间接的)指向非模板基类的指针。有派生类的模板版本。在此模板化的ctor中,实例化派生类,并动态分配此类型的对象。存储的内容是指向非模板基类的指针。

相关问题