访问指针数组中的子类方法

时间:2018-12-29 01:40:28

标签: c++ c++11

我在访问设置为指针数组的对象中的“ getDegreeProgram()”方法时遇到了麻烦;我所有的基类方法都可以使用,但是由于某种原因,我的子类方法甚至不可见。我怀疑我没有正确的语法,并将其所有子类对象转换为学生的基类。

  

roster.h:

   class roster { 
   private:   
   student** classRosterArray; //array of pointers
     

roster.cpp 函数,用于创建我的对象并将其设置为指针数组


   void roster::createStudentObject() {
      classRosterArray = new student *[5]; //array of pointers
   if (degreeProgramInput == "NETWORK") {
      classRosterArray[rosterCounter] = new networkStudent();
   }
   else if (degreeProgramInput == "SECURITY") {
      classRosterArray[rosterCounter] = new securityStudent();
   }
   else classRosterArray[rosterCounter] = new softwareStudent();  
   }
     

student.h 子类(它们是我的基本类“ student”的子类)

    class networkStudent:public student {
    private: 
      int networkDegree;
    public:
      int getDegreeProgram();
      networkStudent();
    };
    class securityStudent:public student {
    private:
      int securityDegree;
    public:
      int getDegreeProgram();
      securityStudent();
    };
    class softwareStudent:public student {
    private:
      int softwareDegree;
    public:
      int getDegreeProgram();
      softwareStudent();
    };    

2 个答案:

答案 0 :(得分:1)

据我了解,您正在尝试访问classRosterArray的元素并尝试调用getDegreeProgram()

对于此问题,请设置getDegreeProgram()虚拟函数。

student.h

class student {
...

public:
    virtual int getDegreeProgram() = 0; // pure virtual function
};

学生的子类

class networkStudent:public student {
private: 
  int networkDegree;
public:
  virtual int getDegreeProgram();
  networkStudent();
};
class securityStudent:public student {
private:
  int securityDegree;
public:
  virtual int getDegreeProgram();
  securityStudent();
};
class softwareStudent:public student {
private:
  int softwareDegree;
public:
  virtual int getDegreeProgram();
  softwareStudent();
};

建议:

在这种情况下,由于getDegreeProgram()似乎是一个getter函数,我认为您应该将其声明为const函数。

编辑:

正如Richard所说的那样,在C ++ 11中,为此子类引入了override关键字。因此,您也可以写virtual int getDegreeProgram();来代替写int getDegreeProgram() override;

答案 1 :(得分:0)

有两种解决方法。

  1. 运行时多态性-此方法将需要较少的代码重构,但要以运行时为代价。一个多态类的每个实例都将具有一个指向虚拟表的不同版本的指针表的指针(vptr)。该表将用于在运行时查找虚拟函数的正确版本。

您可以在此处通过将order by函数虚拟化为基类select n.id, n.title from (select n.*, (@rn := if(@c = category_id, @rn + 1, if(@c := category_id, 1, 1) ) ) as rn from (select n.* from news n order by n.category_id, n.id desc, n.title ) n cross join (select @rn := 0, @c := -1) params ) n where n.rn <= 3; 并在派生类getDegreeProgramStudent中覆盖它来实现运行时多态。和securityStudent

networkStudent
  1. 静态多态性或CRTP或模拟动态绑定-此方法执行与上述相同的操作,但具有通过某种强制转换魔术(在下面)知道编译时类型的优点。甚至这种方法也有其局限性,例如kludgy语法和一些重构,这比第一种情况要大得多,并且由于模板的简洁性等原因导致缺乏可读性。

这里的技巧是在编译时获取派生类的信息,并将基类的softwareStudent指针类型转换为派生类的指针。 :-)

class Student {
...
public:
  virtual int getDegreeProgram() = 0; // notice the virtual keyword and 0 at the end. 
                                      // 0 is for saying that it is pure virtual, meaning
                                      // we don't have any definition for this function in
                                      // this class. Such a class is also called as 
                                      // abstract class
...
}

class securityStudent : Student {
...
public:
 int getDegreeProgram() override 
 {
     // do the stuff you want to do
 }
...
}

//  driver stub
...
 Student *student;
 securityStudent sStudent;
 networkStudent nStudent;
 .
 .
 student = &sStudent;
 student->getDegreeProgram(); // calls security student implementation of getDegreeProgram
 student = &nStudent;
 student->getDegreeProgram(); // calls network student implementation of getDegreeProgram
...