繁体   English   中英

将代码示例从C#转换为C ++

[英]Translating a code example from c# into c++

我正在尝试从以下示例中转换以下内容: SplitContainer Splitter获取焦点矩形?

private: Control getFocused(Control::ControlCollection controls)
{
    //foreach (Control c in controls)
    //{
    //  if (c.Focused)
    //  {
    //      // Return the focused control
    //      return c;
    //  }
    //  else if (c.ContainsFocus)
    //  {
    //      // If the focus is contained inside a control's children
    //      // return the child
    //      return getFocused(c.Controls);
    //  }
    //}
    do
    {
            if (c.Focused)
            {
                // Return the focused control
                return c;
            }
            else if (c.ContainsFocus)
            {
                // If the focus is contained inside a control's children
                // return the child
                return getFocused(c.Controls);
            }
    }
    while(Control c in controls);
   // No control on the form has focus
   return null;
}

我正在为DO WHILE循环寻找合适的Synthanx

while(Control c in controls); // error

并且由于函数' private: Control getFocused(Control::ControlCollection controls) '的类型为Control,因此我需要指定一个返回值,两者都' return null; '和' return nullptr; 失败!

编辑:

for(int index = 0; index <= controls.Count; index++)
        {
            if(controls[index]->Focused)
            {
                return controls[index];
            }
            else if (controls[index]->ContainsFocus)
            {
                return getFocused(controls[index]->Controls);
            }
        }

return controls[index]; -> 不存在从“ System :: Windows :: Forms :: Control ^”到“ System :: Windows :: Forms :: Control”的合适的用户定义转换。

return getFocused(controls[index]->Controls); -> 函数“ getFocused”不能用给定的参数列表调用,参数类型为:(System :: Windows :: Forms :: Control :: ControlCollection ^)

return null; -> 标识符“ null”未定义

要么

return nullptr; -> 从“ decltype(nullptr)”到“ System :: Windows :: Forms :: Control”的用户定义的转换不存在

我的C ++ / CLI有点生锈。 但是,让我大吃一惊:

private: Control ^ getFocused(Control::ControlCollection ^controls)
{
    for each (Control ^c in controls)
    {
        if (c->Focused)
        {
            return c;
        }
        else if (c->ContainsFocus)
        {
            return getFocused(c->Controls);
        }
    }

    return nullptr;
}

MD.Unicorn在技术上是正确的,因为“ foreach”没有C ++语法。 但是,C ++确实在STL中包含非常相似的内容(请考虑使用.net框架而不是C#)。

C ++ STL for_each

for_each(foo.begin(), foo.end(), [=](Control control){
    //Stuff
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM