[英]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;
也。
有兩種解決方法。
您可以在此處通過使getDegreeProgram
函數在基類(即Student
虛擬化,並在派生類(如securityStudent
, networkStudent
和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
...
這里的技巧是在編譯時獲取派生類的信息,並將基類的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.