c++;多态性-将对基类的引用传递给函数 我正在学习C++多态性。在下面的代码中,创建一个名为shape的基类shape类型的指针,然后指向对象r和c
功能<代码>打印区(r)代码>和c++;多态性-将对基类的引用传递给函数 我正在学习C++多态性。在下面的代码中,创建一个名为shape的基类shape类型的指针,然后指向对象r和c,c++,polymorphism,base-class,C++,Polymorphism,Base Class,功能打印区(r)和打印区(c)。但是,在调用这些函数时,shape指向c的地址。那么,当您调用printArea(r)时,它是如何工作的 #包括 使用名称空间std; 阶级形态{ 公众: 虚拟浮点getArea()常量=0; }; 类矩形:公共形状{ 私人: 浮动宽度; 浮动高度; 公众: 矩形(浮动宽度,浮动高度):宽度(宽度),高度(高度){} float getArea()常量{返回宽度*高度;} }; 班级圈子:公共形态{ 私人: 浮动半径; 公众: 圆(浮动半径):半径(半径){} f
打印区(c)然后调用代码>。但是,在调用这些函数时,shape
指向c
的地址。那么,当您调用printArea(r)时,它是如何工作的代码>
#包括
使用名称空间std;
阶级形态{
公众:
虚拟浮点getArea()常量=0;
};
类矩形:公共形状{
私人:
浮动宽度;
浮动高度;
公众:
矩形(浮动宽度,浮动高度):宽度(宽度),高度(高度){}
float getArea()常量{返回宽度*高度;}
};
班级圈子:公共形态{
私人:
浮动半径;
公众:
圆(浮动半径):半径(半径){}
float getArea()常量{return 3.14159f*radius*radius;}
};
空白打印区域(常量形状和形状){
std::cout我猜您的问题是,从技术上讲,对getArea
的调用是如何在运行时调度的
<> P> C++标准不规定这一点,它规定了必须执行哪种实现,而不是如何实现。
几乎所有现存C++实现都是通过在一个或多个虚函数中在类的每个对象中放置一个隐藏指针,该隐藏指针指向一个函数指针表,即指针的对象类的动态类型的虚拟方法。通过检查对象的vtable指针,从vtable中的
getArea
项中检索函数指针,并调用该函数来进行调度
多重继承有一些复杂的因素,但这些只是:复杂因素
另一种方法是搜索每个调用的基类链
这可能效率较低,但至少在其他语言中使用过,例如在最初的Smalltalk中,以及在20世纪90年代Windows中用于Pascal的Borlands GUI类框架中
另一种方法是在每个对象中直接放置指向每个虚函数的指针,本质上是每个对象中的一个vtable。这种方法有时在C中使用。其主要优点是概念上简单。它还避免了一个间接寻址。但它浪费空间,通用性不好。\35; include
#include<iostream>
using namespace std;
class Shape {
public:
virtual float getArea() const = 0;
};
class Rectangle : public Shape {
private:
float width;
float height;
public:
Rectangle(float width, float height) : width(width), height(height) {}
float getArea() const {return width * height;}
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float radius) : radius(radius) {}
float getArea() const {return 3.14159f * radius *radius;}
};
void printArea(const Shape& shape) {
std::cout << "Area: " << shape.getArea() << std::endl;
}
int main() {
Rectangle r(2,6);
Shape* shape = &r; // shape stores the address of object r which when further call
std::cout<< shape <<endl; //called sends its address
Circle c(6);
shape = &c; // same as r is being processed
std::cout<<shape<<endl;
printArea(r); // same thing is taking place here reference is passed and then polymorphism come
printArea(c); // into play
printArea(*shape); // as the shape was given reference of c in the end it returns its value
return 0;
}
使用名称空间std;
阶级形态{
公众:
虚拟浮点getArea()常量=0;
};
类矩形:公共形状{
私人:
浮动宽度;
浮动高度;
公众:
矩形(浮动宽度,浮动高度):宽度(宽度),高度(高度){}
float getArea()常量{返回宽度*高度;}
};
班级圈子:公共形态{
私人:
浮动半径;
公众:
圆(浮动半径):半径(半径){}
float getArea()常量{return 3.14159f*radius*radius;}
};
空白打印区域(常量形状和形状){
std::cout什么工作方式?printArea
需要一个引用,不相关的指针与此有什么关系?它应该工作得很好,Rectangle
和Circle
与Shape
都有“is-a”关系。你试过了吗?“发送给它地址,因此虚拟函数工作”--什么虚拟函数?@Quentin:啊!错在那里写的!现在就修好了!谢谢你让我知道^_^
#include<iostream>
using namespace std;
class Shape {
public:
virtual float getArea() const = 0;
};
class Rectangle : public Shape {
private:
float width;
float height;
public:
Rectangle(float width, float height) : width(width), height(height) {}
float getArea() const {return width * height;}
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float radius) : radius(radius) {}
float getArea() const {return 3.14159f * radius *radius;}
};
void printArea(const Shape& shape) {
std::cout << "Area: " << shape.getArea() << std::endl;
}
int main() {
Rectangle r(2,6);
Shape* shape = &r; // shape stores the address of object r which when further call
std::cout<< shape <<endl; //called sends its address
Circle c(6);
shape = &c; // same as r is being processed
std::cout<<shape<<endl;
printArea(r); // same thing is taking place here reference is passed and then polymorphism come
printArea(c); // into play
printArea(*shape); // as the shape was given reference of c in the end it returns its value
return 0;
}