三星bada开发没有使用指针 [英] samsung bada development without using pointers

查看:181
本文介绍了三星bada开发没有使用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Person p;

Person p("foobar");

Person * p = new Person();

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

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.

来自Bada API参考的示例代码。

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);

注意:使用的Rectangle类会在没有指针的情况下创建一个对象。它和Label有什么不同呢?其混淆: - /

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

推荐答案

在此代码中:

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

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

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();

可以工作,因为默认情况下不会调用析构函数。 pLabel 变量超出范围。

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

这篇关于三星bada开发没有使用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆