在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有什么不同呢?这让人感到困惑 :-/
Person *p = new Person()
是非常糟糕的代码(一般而言)。在这种情况下,p
应该是某种智能指针,可以拥有新分配的Person
。 - Mankarsedelete
已分配的内存。 - Griwesnew
)后跟一个Construct调用。原因是bada(和Symbian)是为资源有限的设备设计的,添加异常处理被认为是太大的开销。IDE可能会提供帮助,但我不知道为什么它会引起OP的问题-也许查看构造函数可能会有所帮助? - Skizz