简体   繁体   English

使用第1层指针调用第3层子类

[英]Calling a 3rd layer subclass using a 1st layer pointer

I made a class hierarchy where the 1st layer super class is staff. 我建立了一个类层次结构,其中第一层超级类是职员。 In my 2nd layer I have 2 subclasses called admin staff and academic staff. 在第二层中,我有2个子类,分别称为管理员和学术人员。 (These two layers are also abstract classes). (这两层也是抽象类)。 Academic staff have 2 more subclasses teaching only and research only. 学术人员还有2个仅用于教学和研究的子类。

In my main, am trying to create an array of 10 staff pointers(5 of admin and 5 of academic) and manipulate 3rd layer subclasses using the pointers. 在我的主要工作中,我试图创建一个由10个职员指针组成的数组(5个管理员和5个学术指针),并使用这些指针操纵第三层子类。 Bare in mind the 3rd layer subclasses have behaviors which are not present in the in 1st or 2nd layer. 切记第三层子类的行为在第一层或第二层中不存在。 Hence, I have to use type casting. 因此,我必须使用类型转换。 I can change my 3rd layer behavior individually like eg, 我可以分别更改第三层的行为,例如,

((ResearchStaff *)ptr[i])->setResearchHour(23);

but it gets very messy very quickly. 但很快就会变得非常混乱。 My question, how can i access my 3rd layer behaviors in a for or while loop without having to individually set them. 我的问题是,如何在for或while循环中访问我的第3层行为,而不必单独设置它们。 Heres the main that im working with. 这是与我合作的主要工具。 Thanks A LOT!! 非常感谢!!

int main()
{
  Staff *ptr[10];
  for(int i=0;i<10;i++)
  {
    ptr[i] = new AdminStaff;
    ((AdminStaff *)ptr[i])->setAdmin(12);

    i++;
    ptr[i] = new TeachingStaff;
    (( TeachingStaff*)ptr[i])->setTeaching(16);
  }
}

Two things! 两件事情!

  1. When you create a new derived class object, in this case AdminStaff or TeachingStaff , you don't have to typecast back to base to invoke a derived class method. 当创建一个新的派生类对象(在本例中为AdminStaffTeachingStaff ,不必强制转换回base即可调用派生类方法。

So instead of, 所以代替

    ptr[i] = new TeachingStaff;
    (( TeachingStaff*)ptr[i])->setTeaching(16);

you can write something like, 你可以这样写

    TeachingStaff *ts = ptr[i] = new TeachingStaff;
    ts->setTeaching(16);
  1. You should only call the base class reference/ pointer to invoke methods which are common to all the classes. 您只应调用基类引用/指针来调用所有类共有的方法。

Finally, if these classes do not have many attributes in common you can consider composition as an alternate design strategy instead of using multiple inheritance. 最后,如果这些类没有很多共同的属性,则可以考虑将组成作为替代设计策略,而不是使用多重继承。

You could use the Visitor pattern. 您可以使用Visitor模式。

Add a method to your Staff class called accept , like this: 在您的Staff类中添加一个名为accept ,如下所示:

class Staff 
{
   //Other staff stuff
   public:
      void accept(const Visitor& v) = 0;
}

Add implementations to your leaf classes... 将实现添加到叶类中...

class ResearchTeacher
{
    //Other research teacher stuff...

    public:
       void accept(const Visitor& v) {
       v.visit(*this);
    }
}

Then define a Visitor that knows what to do with each type: 然后定义一个访问者,该访问者知道如何处理每种类型:

class Visitor
{
   public:
      void visit(ResearchTeacher& teacher) 
      {
         teacher.setResearch(16);
      }

      void visit(TeachingTeacher& teacher)
      {
         teacher.setTeaching(16);
      }

      void visit(AdminStaff& admin) 
      {
         admin.setAdmin(12);
      }
}

Now the calling code doesn't have to worry about it: 现在,调用代码不必担心:

int main()
{
  Visitor v;
  Staff *ptr[10];
  for(int i=0;i<10;i++)
  {
    ptr[i] = new AdminStaff;
    ptr[i]->accept(v);

    i++;
    ptr[i] = new TeachingStaff;
    ptr[i]->accept(v);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的第3叠从我的第1叠和第2叠开始以相反的顺序打印 - My 3rd stack is printing out in reverse order from my 1st and 2nd stack 第一个代码片段与第二个和第三个代码输出不同的可能原因是什么? - What will be the possible causes for the 1st code fragment to output differently than 2nd and 3rd? 获取指向std :: list的第一个元素的指针 - Get pointer to 1st element of std::list 在 c++ 中打印 3 维数组的内容。它的第 1 维来自索引,第 2 维来自另一个数组,第 3 维来自另一个数组? - print the content of 3 dimension array in c++ .. its dimensions 1st come from index and 2nd from another array and 3rd from another array? QT-第三方回拨不回叫? - QT - 3rd Party Callback Not Calling back? 使用函数的第三个参数 - Using the 3rd parameter of a function 指向数组的指针是否不指向数组的第一个元素 - Doesn't a pointer to an array point to 1st element of an array 具有任何嵌套层的数组指针的模板特化 - Template specialization on array pointer with any layer of nesting 将JNI Object作为参考指针访问Java层 - Access JNI Object to Java layer as reference pointer 使用 fakeit 模拟第 3 方库 - Mocking an 3rd party library using fakeit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM