英文:
In C++ what is the correct way to filter a vector of polymorphic derived classes?
问题
以下是翻译好的代码部分:
这里有一个显然简单的示例,可以编译并运行。
#include <iostream>
#include <vector>
using namespace std;
class Shape {
public:
virtual double getArea() = 0;
};
class Circle : public Shape {
public:
double getArea() { return 3.14; }
};
class Rectangle : public Shape {
public:
double getArea() { return 42.0; }
};
int main() {
vector<Shape*> shapes = { new Circle(), new Rectangle() };
for (Shape* s : shapes) { cout << s->getArea() << endl; }
return 0;
}
这段代码的结果是它会打印出向量中的每个形状。
我不知道如何按派生类类型过滤向量。例如,如何迭代遍历向量,并且仅对每个圆执行某些操作?
谢谢你。
英文:
Here is an obviously simple example that compiles and runs.
#include <iostream>
#include <vector>
using namespace std;
class Shape {
public:
virtual double getArea() = 0;
};
class Circle : public Shape {
public:
double getArea() {return 3.14;}
};
class Rectangle : public Shape {
public:
double getArea() {return 42.0;}
};
int main() {
vector<Shape*> shapes = {new Circle(), new Rectangle()};
for (Shape* s : shapes) { cout << s->getArea() << endl;}
return 0;
}
The result is that it will print every Shape in the vector.
What I do not know how to do is to filter the vector by derived class type. For example, how to iterate over the vector and only do something for each Circle?
Thank You
答案1
得分: 5
Here is the translated content:
首先,如果您希望将圆形与其他形状区分对待,您应该考虑将它们简单地存储在其他形状之外。
但有时您希望大部分时间为所有形状执行相同的操作,而有时为圆形执行特殊操作。
在这种情况下,您将执行动态下转型以从指向 Shape
的指针转换为指向 Circle
的指针。如果它指向的 Shape
不是圆形,这将失败并产生一个空指针。
class Circle : public Shape {
public:
double getArea() {return 3.14;}
void circlesOnly() { std::cout << "Whee! I'm a circle!\n"; }
};
vector<Shape*> shapes = {new Circle(), new Rectangle()};
for (Shape *shape : shapes) {
if (Circle *c = dynamic_cast<Circle *>(shape); c != nullptr)
c->circlesOnly();
}
请注意,代码部分未进行翻译,仅提供原文。
英文:
First of all, if you want to treat Circle's differently from other shapes, you should consider simply storing them separately from other shapes.
But sometimes you want to do the same things for all shapes most of the time, and something special for Circles some of the time.
In such a case, you'd do a dynamic down-cast to get from a pointer to Shape
to pointer to Circle
. If the Shape
it points to isn't a circle, this will fail and yield a null pointer.
class Circle : public Shape {
public:
double getArea() {return 3.14;}
void circlesOnly() { std::cout << "Whee! I'm a circle!\n"; }
};
vector<Shape*> shapes = {new Circle(), new Rectangle()};
for (Shape *shape : shapes) {
if (Circle *c = dynamic_cast<Circle *>(shape); c != nullptr)
c->circlesOnly();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论