使用基 class 指针访问派生 class(前向声明)

Access Derived class (forward declaration) using base class pointer

如何从以下场景访问我的单元测试框架(即 Gtest_main.cpp)中派生的 class 成员变量。

imp.cpp

   class base{
        public:
            virtual void foo()=0;
    };
    
    class derived : public base{
        public:
           int abc; // how to access this from Gtest_main.cpp
            void foo(){
                std::cout<<"This is derived foo() \n";
            }
    }; 

//Unit test framework call on createPlugin to get instance of derived class
  void createPlugin(base **plugin){
    derived *dp = new derived;
    *plugin=dp;
 }

Gtest_main.cpp(单元测试文件)

 class derived //forward declaration 
 base *p;
 createPlugin(&p);
 static_cast<derived*>(p)->abc=9090; //error how to access abc?

如何在 Gtest_main.cpp 中访问 abc。请注意我无法修改原始源代码impl.cpp

How to access abc in Gtest_main.cpp

你不能。没有看到 class derived 的完整定义,编译器不知道 derived::abc 是什么意思。