簡體   English   中英

訪問指針數組中的子類方法

[英]Accessing subclass methods in array of pointers

我一直在訪問設置為指針數組的對象中的“ 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(); }; 

據我了解,您正在嘗試訪問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();編寫virtual int getDegreeProgram(); ,您可以編寫int getDegreeProgram() override; 也。

有兩種解決方法。

  1. 運行時多態-此方法將需要較少的代碼重構,但要以運行時為代價。 一個多態類的每個實例都將具有一個指向虛擬表的不同版本的指針表的指針(vptr)。 該表將用於在運行時查找虛擬函數的正確版本。

您可以在此處通過使getDegreeProgram函數在基類(即Student虛擬化,並在派生類(如securityStudentnetworkStudentsoftwareStudent重寫它來實現運行時多態。

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
...
  1. 靜態多態性或CRTP或模擬動態綁定-此方法執行與上述相同的操作,但具有通過某種強制轉換魔術(在下面)知道編譯類型的優點。 即使這種方法也有其局限性,例如糊塗的語法和一些重構,這比第一種情況要大得多,並且由於模板的簡潔性等原因導致缺乏可讀性。

這里的技巧是在編譯時獲取派生類的信息,並將基類的this指針類型轉換為派生類的指針。 :-)

template <typename StudentType>
class Student {
...
public:
  int getDegreeProgram() 
  {
     return (static_cast<StudentType*>(this))->getDegreeProgramImpl();
  }
...
}

class securityStudent : public Student<securityStudent> {
...
public:
  int getDegreeProgramImpl()
  {
    // write your specifc implementation here
  }
...
}

// driver stub
...
 securityStudent sStudent;
 networkStudent nStudent;
 .
 .
 sStudent.getDegreeProgram(); // calls security student implementation of getDegreeProgram
 nStudent.getDegreeProgram(); // calls network student implementation of getDegreeProgram
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM