【问题标题】:What do conditionals do to polymorphic objects in C++? (inclusion polymorphism)条件对 C++ 中的多态对象有什么作用? (包含多态性)
【发布时间】:2015-12-05 04:23:05
【问题描述】:

我遇到了一个有趣的错误,我很确定它与条件语句上下文中的包含多态性有关。

示例重点如下:

   ClassParent *parentPointer; //Declare pointer to parent

   if(condition){
       ClassChild1   = mychild; //Declare child1 object
       parentPointer = *mychild;//Parent pointer points to child
   }

   if(!condition){
       ClassChild2   = mychild; //Declare child2
       parentPointer = *mychild;//Parent pointer points to child2  
   }

   cout << *parentPointer; //What will this point to???

应该清楚,条件语句使 *parentPointer 变量在最后一行。

我的整个函数看起来像这样:(注意它崩溃的地方)

    void PosApp::addItem(bool isPerishable) {
        Item *refitem;

        if (isPerishable) {
            Perishable myitem;
            std::cout   << "Enter the following: "  << std::endl
                        << "Sku: "                  << std::endl
                        << "Name:"                  << std::endl
                        << "Price: "                << std::endl
                        << "Taxed: "                << std::endl
                        << "Quantity: "             << std::endl
                        << "Expiry date: "          << std::endl;

            std::cin    >> myitem;
            refitem = &myitem; //Item now implements inclusion polymorphism,  be aware of dynamic/static types (dynamic is item, static Perishable)
        }

        if (!isPerishable) {
            NonPerishable myitem;
            std::cout   << "Enter the following: "  << std::endl
                        << "Sku: "                  << std::endl
                        << "Name:"                  << std::endl
                        << "Price: "                << std::endl
                        << "Taxed: "                << std::endl
                        << "Quantity: "             << std::endl;

            std::cin    >> myitem;
            refitem = &myitem; //Item now implements inclusion polymorphism,  be aware of dynamic/static types (dynamic is item, static NonPerishable)


        }

        if (cin.fail()) {//The inclusion polymorphism allows me to call this block only once regardless of persh/non-perishable
            cin.clear();
            cin.ignore(2000, '\n');

            //CRASH POINT***********
            cout << "Error: " << *refitem << endl;//Be aware of early/late binding, the write/dowrite must be child calls, not parent.
        }


    }

现在非常有趣的是,当删除 cin.fail 上的 if() 并强制输入错误时,它可以工作。现在的代码如下所示:

    void PosApp::addItem(bool isPerishable) {
        Item *refitem;


        if (!isPerishable) {
            NonPerishable myitem;
            std::cout   << "Enter the following: "  << std::endl
                        << "Sku: "                  << std::endl
                        << "Name:"                  << std::endl
                        << "Price: "                << std::endl
                        << "Taxed: "                << std::endl
                        << "Quantity: "             << std::endl;

            std::cin    >> myitem;
            refitem = &myitem; //Item now implements inclusion polymorphism,  be aware of dynamic/static types (dynamic is item, static NonPerishable)


            cin.clear();
            cin.ignore(2000, '\n');

            //THIS DOES NOT CRASH NOW
            cout << "Error: " << *refitem << endl;//Be aware of early/late binding, the write/dowrite must be child calls, not parent.



    }

就崩溃而言,我能想到的最佳答案是,当范围在第一个代码 sn-p 中解析时,程序以某种方式丢失了指针的内容。

这个问题有两个方面:你能在条件语句的上下文中实现包含多态性(如图所示)吗?如果不能,这是导致我的程序崩溃的原因吗?

注意:我没有包含整个程序(因为它有数百行),但我只想说,当我将代码更改为第二个 sn-p 时,行为应该是预期的。

【问题讨论】:

    标签: c++ oop polymorphism conditional inclusion


    【解决方案1】:

    具有自动存储功能的对象在它们周围的 { } 大括号中是本地的,包括 if 语句。如果您有一个指向本地的指针,并且该对象超出范围,则访问该指针是 UB。

    Object* ptr;
    if (condition)
    {
        Object obj;
        ptr = &obj;
    } //obj is out of scope
    *ptr; //undefined behaviour
    

    这就是您将refitem 设置为指向本地对象所做的事情。相反,使用new 创建Perishable*NonPerishable*,当块结束时,将该指针分配给refitem。多态性将按您的预期工作,错误只是对象的范围。

    if (!isPerishable)
    {
        NonPerishable* myitem = new NonPerishable(); //dynamic memory
        std::cin >> *myitem;
        refitem = myitem; //refitem is still valid after this scope ends
    }
    

    【讨论】:

    • 真的是UB吗?我原以为这是一个直接错误。
    • 一切似乎都正确,只是我必须包含一个遵从运算符 (*) 以使 cin >> 匹配。我提出了修改建议。
    • @yzt 老实说,我希望编译器能够为此发出警告,但显然不是。
    • @JamesRoot - 遗憾的是,C++ 不像 Java。
    • @jamesRoot 我在那里添加了另一个编辑并包括一个删除调用。我认为这需要手动进行,用 new 初始化时?
    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多