是否可以为复制基的虚函数提供不同的定义?
Is it possible to provide different definitions for a virtual function of a replicated base?
我知道声明,
"A virtual function of a replicated base class can be overridden by a (single) function in a derived class."
我很好奇是否可以提供多个定义,以防万一——如何正确定义和调用?
为了更清楚一点,
我的 classes 和 class 层次结构就像
A A
| |
B C
\ /
D
//i.e., I am not deriving B and C from a virtual base A.
class A {
virtual void f(){
cout<<"belongs to A"<<endl;
}
}
class B: public A {
void f(){
cout<<"belongs to B"<<endl;
}
}
class C: public A {
void f(){
cout<<"belongs to C"<<endl;
}
}
class D: public B, public C {
void f(){ //This overrides definitions B::f() and C::f()
cout<<"belongs to D"<<endl;
}
}
B* b;
C* c;
D d;
b = &d;
c = &d;
b->f();//This would output "belongs to D"
c->f();//This also would output "belongs to D"
//Now I want to redefine f() in D like this
class D: public B, public C {
void B::f() {
cout<<"belongs to D::B::f()"<<endl;
}
void C::f() {
cout<<"belongs to D::C::f()"<<endl;
}
}
//So that
B* b;
C* c;
D d;
b = &d;
c = &d;
b->f();//This would output "belongs to D::B::f()"
c->f();//This also would output "belongs to D::C::f()"
在某些时候,您需要能够告诉编译器您要使用哪个实现。
这样做的一种方法:
A
/ \
B C
\ /
D
struct A
{
virtual void f();
};
struct B : public A
{
void f() override;
};
struct C : public A
{
void f() override;
};
struct D : public B,C
{
};
int test(int num) {
D d;
d.f(); // undefined
d.A::f(); // Impossible because conversion from D to A is ambiguous
d.B::f(); // calls B implementation
d.C::f(); // calls C implementation
}
嗯,下面是一个虚函数的基本例子
class Person
{
int age;
public:
Person(int=0);
virtual void describe() = 0;
int getAge();
};
Person :: Person(int age)
{
this->age = age;
}
int Person :: getAge()
{
return this->age;
}
class Student : public Person
{
public:
Student(int=0);
void describe();
};
Student :: Student(int age) : Person(age) {}
void Student :: describe()
{
std::cout << "Hi, I am a student, ";
std::cout << this->getAge() << " years old!";
}
class Employee : public Student
{
public:
Employee(int=0);
void describe();
};
Employee :: Employee(int age) : Person(age) {}
void Employee :: describe()
{
std::cout << "Hi, I am an employee, ";
std::cout << this->getAge() << " years old!";
}
简短的回答是否。 – 好奇的家伙
@curiousguy 在评论中提供的答案。
我知道声明, "A virtual function of a replicated base class can be overridden by a (single) function in a derived class." 我很好奇是否可以提供多个定义,以防万一——如何正确定义和调用?
为了更清楚一点, 我的 classes 和 class 层次结构就像
A A
| |
B C
\ /
D
//i.e., I am not deriving B and C from a virtual base A.
class A {
virtual void f(){
cout<<"belongs to A"<<endl;
}
}
class B: public A {
void f(){
cout<<"belongs to B"<<endl;
}
}
class C: public A {
void f(){
cout<<"belongs to C"<<endl;
}
}
class D: public B, public C {
void f(){ //This overrides definitions B::f() and C::f()
cout<<"belongs to D"<<endl;
}
}
B* b;
C* c;
D d;
b = &d;
c = &d;
b->f();//This would output "belongs to D"
c->f();//This also would output "belongs to D"
//Now I want to redefine f() in D like this
class D: public B, public C {
void B::f() {
cout<<"belongs to D::B::f()"<<endl;
}
void C::f() {
cout<<"belongs to D::C::f()"<<endl;
}
}
//So that
B* b;
C* c;
D d;
b = &d;
c = &d;
b->f();//This would output "belongs to D::B::f()"
c->f();//This also would output "belongs to D::C::f()"
在某些时候,您需要能够告诉编译器您要使用哪个实现。
这样做的一种方法:
A
/ \
B C
\ /
D
struct A
{
virtual void f();
};
struct B : public A
{
void f() override;
};
struct C : public A
{
void f() override;
};
struct D : public B,C
{
};
int test(int num) {
D d;
d.f(); // undefined
d.A::f(); // Impossible because conversion from D to A is ambiguous
d.B::f(); // calls B implementation
d.C::f(); // calls C implementation
}
嗯,下面是一个虚函数的基本例子
class Person
{
int age;
public:
Person(int=0);
virtual void describe() = 0;
int getAge();
};
Person :: Person(int age)
{
this->age = age;
}
int Person :: getAge()
{
return this->age;
}
class Student : public Person
{
public:
Student(int=0);
void describe();
};
Student :: Student(int age) : Person(age) {}
void Student :: describe()
{
std::cout << "Hi, I am a student, ";
std::cout << this->getAge() << " years old!";
}
class Employee : public Student
{
public:
Employee(int=0);
void describe();
};
Employee :: Employee(int age) : Person(age) {}
void Employee :: describe()
{
std::cout << "Hi, I am an employee, ";
std::cout << this->getAge() << " years old!";
}
简短的回答是否。 – 好奇的家伙
@curiousguy 在评论中提供的答案。