C++项目类型

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

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 = &#39;x&#39;;
	string name = &quot;Undefined&quot;;
	string type = &quot;Undefined&quot;;
	string description = &quot;Undefined&quot;;
public:
	char getLook() const {return look;}
	string getType() const {return type;}
	string getName() const {return name;}
	string getDescription() const {return description;}
	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;);
	virtual string toString() const;
};

class TallProduct : public SimpleProduct {
public:
	TallProduct(char look, string &amp;&amp;name = &quot;Undefined&quot;, string &amp;&amp;type = &quot;Undefined&quot;, string &amp;&amp;description = &quot;Undefined&quot;);
	string toString() const;
};

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:

if (y != 0 &amp;&amp; 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&lt;&gt;, 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 &lt;&lt; (dynamic_cast&lt;B*&gt;(b) != nullptr) &lt;&lt; &quot;\n&quot;;
    std::cout &lt;&lt; (dynamic_cast&lt;B*&gt;(c) != nullptr) &lt;&lt; &quot;\n&quot;;
}

which creates following output:

1
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:

确定