Skip to content

Latest commit

 

History

History
23 lines (19 loc) · 547 Bytes

File metadata and controls

23 lines (19 loc) · 547 Bytes

编写与上一个练习类似的转换,这一次将 Query_base 对象转换为 AndQuery 的引用。 重复上面的测试过程,确保转换能正常工作。

答:AndQuery 使用私有构造函数,无法为本题编写可编译的代码。

AndQuery aq(Query("hair"), Query("Alice"));
Query_base &rqb = aq;

try
{
    AndQuery &raq = dynamic_cast<AndQuery &>(rqb);

    if (typeid(raq) == typeid(AndQuery))
    {
        cout << "success" << endl;
    }
}
catch (bad_cast)
{
    cout << "fail" << endl;
}