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 :-/
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.