【问题标题】:Translating a code example from c# into c++将代码示例从 c# 翻译成 c++
【发布时间】:2013-03-18 17:48:45
【问题描述】:

我正在尝试从以下示例中翻译以下内容 SplitContainer Splitter Gets Focus Rectangle?

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 循环寻找合适的合成器

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]-&gt;Controls); -> 不能使用给定的参数列表参数类型调用函数“getFocused”:(System::Windows::Forms::Control::ControlCollection ^)

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

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

【问题讨论】:

  • 你曾经用谷歌搜索过代码或语法吗?我只是在谷歌上搜索了 --- 在 c++ 中并找到了 MSDN 参考 msdn.microsoft.com/en-us/library/b0kk5few(v=vs.80).aspx
  • 是的,我做到了……但是……这主要是标准运算符的默认教程
  • C++ 中没有foreach(.. in ..) 语法。您必须使用普通的for 循环使用索引来访问项目。

标签: c# winforms visual-c++ c++-cli


【解决方案1】:

我的 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;
}

【讨论】:

  • Control '^' getFocused(Control::ControlCollection ^controls) 这样做的诀窍是在方法前面添加“^”...制作函数指针?
  • 您不是在创建函数指针,而是通过句柄传递和返回 CLR 对象(在 C# 中自动完成,但在 C++/CLI 中必须显式)。
【解决方案2】:

MD.Unicorn 在技术上是正确的,因为“foreach”没有 C++ 语法。然而,C++ 确实在 STL 中包含了一些非常相似的东西(想想 .net 框架而不是 C#)。

C++ STL for_each

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

【讨论】:

  • 我知道“for each”循环不是 c++ 合成器,这就是我问这个问题的原因。
  • Erm...sigh...我的意思是,在 C++ 中它不在语言中,它在标准库中。这是该死的标准,它唯一的技术上不是语言的一部分。
  • 这是 STL:for_each 的正确语法。这不能回答你的问题。但只是解决了你的问题。
  • 它可能,如果我在 win32 上编码......但我使用的是 dotnot
猜你喜欢
  • 2011-09-05
  • 1970-01-01
  • 2016-08-04
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多