英文:
C++ type of item
问题
以下是您的代码的翻译部分:
using namespace std;
class SimpleProduct {
char look = 'x';
string name = "Undefined";
string type = "Undefined";
string description = "Undefined";
public:
char getLook() const {return look;}
string getType() const {return type;}
string getName() const {return name;}
string getDescription() const {return description;}
SimpleProduct(char look = 'x', string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
virtual string toString() const;
};
class TallProduct : public SimpleProduct {
public:
TallProduct(char look, string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
string toString() const;
};
所有产品都保存在另一个类的属性 `unique_ptr<Product> ***products;` 中,我想检查某个特定产品是否是 Product 或 TallProduct 类型。
例如:
```cpp
if (y != 0 && products[x][y-1][position] is type TallProduct) { // 伪代码
return 0;
}
英文:
Here is my code:
using namespace std;
class SimpleProduct {
char look = 'x';
string name = "Undefined";
string type = "Undefined";
string description = "Undefined";
public:
char getLook() const {return look;}
string getType() const {return type;}
string getName() const {return name;}
string getDescription() const {return description;}
SimpleProduct(char look = 'x', string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
virtual string toString() const;
};
class TallProduct : public SimpleProduct {
public:
TallProduct(char look, string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
string toString() const;
};
All of products are saved in attribute unique_ptr<Product> ***products;
of another class and I want to check, whether some specific product is type Product or TallProduct.
So for example:
if (y != 0 && products[x][y-1][position] is type TallProduct) { //pseudocode
return 0;
}
答案1
得分: 3
如果你使用 dynamic_cast<>
,你可以检查类型是否是特定的,如果它是继承的并且是多态的。
这是一个小示例:
class A {
public:
virtual ~A() {};
};
class B : public A {};
class C : public A {};
int main() {
A* b = new B();
C* c = new C();
std::cout << (dynamic_cast<B*>(b) != nullptr) << "\n";
std::cout << (dynamic_cast<B*>(c) != nullptr) << "\n";
}
这会产生以下输出:
1
0
这意味着 b
的类型是 B
,但 c
的类型不是。
英文:
If you use dynamic_cast<>
, you can check if type is something specific if it is inherited and it is polymorphic.
Here is a small sample:
class A {
public:
virtual ~A() {};
};
class B : public A {};
class C : public A {};
int main() {
A* b = new B();
C* c = new C();
std::cout << (dynamic_cast<B*>(b) != nullptr) << "\n";
std::cout << (dynamic_cast<B*>(c) != nullptr) << "\n";
}
which creates following output:
1
0
That means b
is from type B
but c
is not.
答案2
得分: 0
为使此工作正常,您需要执行两项操作:
- 在您的SimpleProduct中至少添加一个虚函数,以便层次结构有一个虚函数表(vtable)。您需要在这里添加虚拟析构函数,以使
unique_ptr
能够正常工作:virtual ~SimpleProduct() = default
- 使用dynamic_cast的指针变体来检查转换,即
auto tallProduct = dynamic_cast<TallProduct*>(products[x][y-1][position]);
如果指针不是nullptr
,则可以继续进行
请注意,这通常被认为是不良风格,因为它违反了面向对象编程的“告诉,不要询问”原则。
英文:
To get this to work, you need to do two things:
- Add at least one virtual function to your SimpleProduct, so that the hierarchy gets a vtable. You want the virtual destructor here, so that
unique_ptr
will do it's job properly:virtual ~SimpleProduct() = default
- Check the conversion with the pointer-variant of dynamic_cast, i.e.
auto tallProduct = dynamic_cast<TallProduct*>(products[x][y-1][position]);
You can proceed if the pointer is notnullptr
Note that this is generally considered bad style, because it violates the "Tell, don't ask" mantra of OOP
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论