在 parent 中为 child class 变量定义 getter
Define getter for child class variable inside parent
我有一个项目,我正在使用指向 parent class 的指针访问多个 classes。我需要知道指针指的是哪个 child class,所以我将一个不同的整数 ID 号作为常量 class 变量添加到每个指针中。
我需要使用指针访问这个变量,所以我为那个变量写了一个 getter 函数。由于 getter 函数的代码对于所有 child class 都是相同的,因此我尝试在 parent class 中定义该函数。这大致导致了下面的代码:
class Parent {
public:
virtual void func();
// Getter function
uint8_t getID() {
return classID;
}
// Set ID to a default value
const uint8_t classID = 0;
};
// One of many child classes
class A: public Parent {
public:
void func() {//do something}
const uint8_t classID = 1;
};
int main(){
Parent* childPointer = new A;
uint8_t currentID = childPointer -> getID();
}
然而,运行 这导致 currentID
等于 Parent.classID
而不是 A.classID
。
使 getID
函数成为虚拟函数并在每个 child classes 中对其进行定义使其按预期工作,但这会导致相当多的重复代码。我想知道是否有一种方法可以在 Parent
class 中定义一次 getter 函数并使其成为 return 正确的值?否则,有更清洁的方法吗?
在 C++ 中,每个 class 都有自己的命名空间。在您的代码中,Parent 命名空间中有一个 classID,A 命名空间中有一个 classID。所以 A 的每个实例实际上都有 2 个 classID。不过,getID() 函数只能看到 Parent 命名空间中的 classID。这就是为什么它只有 returns 0.
更好的方法是定义构造函数,它会在 Parent:
中初始化 classID
#include <cstdint>
class Parent {
public:
Parent(): classID(0){}; //Default Parent initializes with 0
Parent(uint8_t ID): classID(ID){}; //Constructor to be called by children
virtual void func(){};
// Getter function
uint8_t getID() {
return classID;
};
const uint8_t classID;
};
// One of many child classes
class A: public Parent {
public:
A(): Parent(1){}; //Every A calls Parent(uint8_t) constructor
void func() {};//do something
//const uint8_t classID = 1; //This is not needed anymore
};
int main(){
Parent* childPointer = new A;
uint8_t currentID = childPointer -> getID();
}
我有一个项目,我正在使用指向 parent class 的指针访问多个 classes。我需要知道指针指的是哪个 child class,所以我将一个不同的整数 ID 号作为常量 class 变量添加到每个指针中。
我需要使用指针访问这个变量,所以我为那个变量写了一个 getter 函数。由于 getter 函数的代码对于所有 child class 都是相同的,因此我尝试在 parent class 中定义该函数。这大致导致了下面的代码:
class Parent {
public:
virtual void func();
// Getter function
uint8_t getID() {
return classID;
}
// Set ID to a default value
const uint8_t classID = 0;
};
// One of many child classes
class A: public Parent {
public:
void func() {//do something}
const uint8_t classID = 1;
};
int main(){
Parent* childPointer = new A;
uint8_t currentID = childPointer -> getID();
}
然而,运行 这导致 currentID
等于 Parent.classID
而不是 A.classID
。
使 getID
函数成为虚拟函数并在每个 child classes 中对其进行定义使其按预期工作,但这会导致相当多的重复代码。我想知道是否有一种方法可以在 Parent
class 中定义一次 getter 函数并使其成为 return 正确的值?否则,有更清洁的方法吗?
在 C++ 中,每个 class 都有自己的命名空间。在您的代码中,Parent 命名空间中有一个 classID,A 命名空间中有一个 classID。所以 A 的每个实例实际上都有 2 个 classID。不过,getID() 函数只能看到 Parent 命名空间中的 classID。这就是为什么它只有 returns 0.
更好的方法是定义构造函数,它会在 Parent:
中初始化 classID#include <cstdint>
class Parent {
public:
Parent(): classID(0){}; //Default Parent initializes with 0
Parent(uint8_t ID): classID(ID){}; //Constructor to be called by children
virtual void func(){};
// Getter function
uint8_t getID() {
return classID;
};
const uint8_t classID;
};
// One of many child classes
class A: public Parent {
public:
A(): Parent(1){}; //Every A calls Parent(uint8_t) constructor
void func() {};//do something
//const uint8_t classID = 1; //This is not needed anymore
};
int main(){
Parent* childPointer = new A;
uint8_t currentID = childPointer -> getID();
}