三星bada开发不使用指针

发布于 2024-12-05 11:27:00 字数 814 浏览 2 评论 0原文

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

Person p;

or

Person p("foobar");

or

Person * p = new Person();

那么,为什么 Samsung 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有什么不同呢?它令人困惑:-/

Objects in C++ can be created using the methods listed below(that I am aware of):

Person p;

or

Person p("foobar");

or

Person * p = new Person();

Then, why does not the Samsung Bada IDE allow me to do the first two methods? Why do I always have to use pointers? I am ok with using pointers and all, just that I want to know the fundamental reason behind the style.

Sample code from Bada API reference.

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

I modified and tried using the code below. Although it compiles and the app runs, the label does not show up on the form.

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

Note : Rectangle class which is used creates an object on the fly without pointer. How is it different from Label then? Its confusing :-/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温馨耳语 2024-12-12 11:27:00

在此代码中:

// 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 变量超出范围时,默认情况下不会调用析构函数。

In this code:

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

the label object is destroyed when it goes out of scope and hence it fails to show up on the form. It is unfortunate that the AddControl method takes a reference as that implies that the above should work. Using:

Label *pLabel = new Label();

works because the destructor is not called by default when the pLabel variable goes out of scope.

递刀给你 2024-12-12 11:27:00

请注意 Bada文档是这么说的

所有容器和控件都必须在设备堆内存上创建。当应用程序终止时,bada 平台会删除框架控件及其后代。此外,平台还释放已分配给应用程序的堆内存。

您的代码可能运行良好,因为它会忽略对 AddControl 的调用,因为它检测到该控件未在堆空间中分配。在这种情况下,AddControl 应该返回一个错误代码。

要获得正确的 Bada 代码,需要编写如下内容:

result MySample::OnInitializing() {
  result r = E_SUCCESS;

  std::auto_ptr<Label> pLabel(new Label);
  if (pLabel.get() != null && E_SUCESS == pLabel->Construct(Rectangle(50, 200, 150, 40), L"Text"))
  {
     pLabel->SetBackgroundColor(Color::COLOR_BLUE);
     r = AddControl(*pLabel);
     pLabel.release();
  }
  else
  {
    r = E_FAILURE;
  }
  return r;

}

遵循此类编码指南可确保您的应用程序能够在初始化 UI 时升级任何问题,除非在执行 OnAppInitializing 时发生问题。

Note that Bada documentation says this

All containers and controls must be created on the device heap memory. When an application terminates, the bada platform deletes the frame control and its descendants. Furthermore, the platform frees the heap memory that has been allocated to the application.

Your code may run fine because it ignores the call to AddControl because it detected that the control was not allocated in the heap space. In that case AddControl should have returned an error code.

To get a correct Bada code, one needs to write something like:

result MySample::OnInitializing() {
  result r = E_SUCCESS;

  std::auto_ptr<Label> pLabel(new Label);
  if (pLabel.get() != null && E_SUCESS == pLabel->Construct(Rectangle(50, 200, 150, 40), L"Text"))
  {
     pLabel->SetBackgroundColor(Color::COLOR_BLUE);
     r = AddControl(*pLabel);
     pLabel.release();
  }
  else
  {
    r = E_FAILURE;
  }
  return r;

}

Following such coding guidelines ensures that your application will be able to escalate any issues while initializing UI except if the issue happens whiele executing OnAppInitializing.

最好是你 2024-12-12 11:27:00

访客在他的评论中指出了这一点; Bada 使用 Symbian 在九十年代中期首创的 C++ 技术。今天它们已经完全过时了。别太认真了,你真的不能指望三星会把他们的顶级人才投入到这样一个注定失败的项目中。这些将在他们的 Android 系列上运行。

Visitor nailed it in his comment; Bada uses C++ techniques pioneered in the mid-nineties by Symbian. They're utterly outdated today. Don't take it too seriously, you can't really expect Samsung to put their top-rate people on such a doomed project. Those will be working on their Android line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文