问题描述
可以使用下面列出的方法(我知道)创建 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开发不使用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!