C++项目类型

huangapple go评论114阅读模式
英文:

C++ type of item

问题

以下是您的代码的翻译部分:

  1. using namespace std;
  2. class SimpleProduct {
  3. char look = 'x';
  4. string name = "Undefined";
  5. string type = "Undefined";
  6. string description = "Undefined";
  7. public:
  8. char getLook() const {return look;}
  9. string getType() const {return type;}
  10. string getName() const {return name;}
  11. string getDescription() const {return description;}
  12. SimpleProduct(char look = 'x', string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
  13. virtual string toString() const;
  14. };
  15. class TallProduct : public SimpleProduct {
  16. public:
  17. TallProduct(char look, string &&name = "Undefined", string &&type = "Undefined", string &&description = "Undefined");
  18. string toString() const;
  19. };
  20. 所有产品都保存在另一个类的属性 `unique_ptr<Product> ***products;` 中,我想检查某个特定产品是否是 Product TallProduct 类型。
  21. 例如:
  22. ```cpp
  23. if (y != 0 && products[x][y-1][position] is type TallProduct) { // 伪代码
  24. return 0;
  25. }
英文:

Here is my code:

  1. using namespace std;
  2. class SimpleProduct {
  3. char look = &#39;x&#39;;
  4. string name = &quot;Undefined&quot;;
  5. string type = &quot;Undefined&quot;;
  6. string description = &quot;Undefined&quot;;
  7. public:
  8. char getLook() const {return look;}
  9. string getType() const {return type;}
  10. string getName() const {return name;}
  11. string getDescription() const {return description;}
  12. SimpleProduct(char look = &#39;x&#39;, string &amp;&amp;name = &quot;Undefined&quot;, string &amp;&amp;type = &quot;Undefined&quot;, string &amp;&amp;description = &quot;Undefined&quot;);
  13. virtual string toString() const;
  14. };
  15. class TallProduct : public SimpleProduct {
  16. public:
  17. TallProduct(char look, string &amp;&amp;name = &quot;Undefined&quot;, string &amp;&amp;type = &quot;Undefined&quot;, string &amp;&amp;description = &quot;Undefined&quot;);
  18. string toString() const;
  19. };

All of products are saved in attribute unique_ptr&lt;Product&gt; ***products; of another class and I want to check, whether some specific product is type Product or TallProduct.

So for example:

  1. if (y != 0 &amp;&amp; products[x][y-1][position] is type TallProduct) { //pseudocode
  2. return 0;
  3. }

答案1

得分: 3

如果你使用 dynamic_cast<>,你可以检查类型是否是特定的,如果它是继承的并且是多态的

这是一个小示例:

  1. class A {
  2. public:
  3. virtual ~A() {};
  4. };
  5. class B : public A {};
  6. class C : public A {};
  7. int main() {
  8. A* b = new B();
  9. C* c = new C();
  10. std::cout << (dynamic_cast<B*>(b) != nullptr) << "\n";
  11. std::cout << (dynamic_cast<B*>(c) != nullptr) << "\n";
  12. }

这会产生以下输出:

  1. 1
  2. 0

这意味着 b 的类型是 B,但 c 的类型不是。

英文:

If you use dynamic_cast&lt;&gt;, you can check if type is something specific if it is inherited and it is polymorphic.

Here is a small sample:

  1. class A {
  2. public:
  3. virtual ~A() {};
  4. };
  5. class B : public A {};
  6. class C : public A {};
  7. int main() {
  8. A* b = new B();
  9. C* c = new C();
  10. std::cout &lt;&lt; (dynamic_cast&lt;B*&gt;(b) != nullptr) &lt;&lt; &quot;\n&quot;;
  11. std::cout &lt;&lt; (dynamic_cast&lt;B*&gt;(c) != nullptr) &lt;&lt; &quot;\n&quot;;
  12. }

which creates following output:

  1. 1
  2. 0

That means b is from type B but c is not.

答案2

得分: 0

为使此工作正常,您需要执行两项操作:

  1. 在您的SimpleProduct中至少添加一个虚函数,以便层次结构有一个虚函数表(vtable)。您需要在这里添加虚拟析构函数,以使unique_ptr能够正常工作:virtual ~SimpleProduct() = default
  2. 使用dynamic_cast的指针变体来检查转换,即auto tallProduct = dynamic_cast<TallProduct*>(products[x][y-1][position]); 如果指针不是nullptr,则可以继续进行

请注意,这通常被认为是不良风格,因为它违反了面向对象编程的“告诉,不要询问”原则。

英文:

To get this to work, you need to do two things:

  1. 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
  2. Check the conversion with the pointer-variant of dynamic_cast, i.e. auto tallProduct = dynamic_cast&lt;TallProduct*&gt;(products[x][y-1][position]); You can proceed if the pointer is not nullptr

Note that this is generally considered bad style, because it violates the "Tell, don't ask" mantra of OOP

huangapple
  • 本文由 发表于 2020年1月3日 22:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定