微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 三星bada开发没有使用指针

C中的对象可以使用下面列出的方法创建(我知道):

Person p;

要么

Person p("foobar");

要么

Person * p = new Person();

那么,为什么三星Bada IDE不允许我做前两种方法呢?为什么我总是要使用指针?我可以使用指针和所有,只是我想知道风格背后的根本原因.

Bada API参考的示例代码.

// Create a Label
Label *pLabel = new Label();
pLabel->Construct(Rectangle(50, 200, 150, 40), L"Text");
pLabel->SetBackgroundColor(Color::COLOR_BLUE);
AddControl(*pLabel);

修改并尝试使用下面的代码.虽然它会编译并运行应用程序,但标签不会显示在表单上.

// Create a Label
Label pLabel();
pLabel.Construct(Rectangle(50, 200, 150, 40), L"Text");
pLabel.SetBackgroundColor(Color::COLOR_BLUE);
AddControl(pLabel);

注意:使用的Rectangle类在没有指针的情况下动态创建对象.它与Label有何不同?令人困惑: – /

解决方法:

在这代码中:

// Create a Label
Label pLabel;
pLabel.Construct(Rectangle(50, 200, 150, 40), L"Text");
pLabel.SetBackgroundColor(Color::COLOR_BLUE);
AddControl(pLabel);

标签对象超出范围时会被销毁,因此无法显示在表单上.不幸的是,AddControl方法接受引用,因为这意味着上述应该起作用.使用:

Label *pLabel = new Label();

可行,因为当pLabel变量超出范围时,认情况下不会调用析构函数.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐