I can write a function this way in Objective-C. This can be used to churn out many UIButtons.
+(UIButton*)getButton:(CGRect)frame{
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Some title" forState:UIControlStateNormal];
button.frame=frame;
return button;
}
Can the same be done in C++? I am not asking about creation of UIButton in C++.
But churning out many objects with a help of a function as this:
CString getCstring(some parameters)
{
CString string = L"Hi sampe string.";
return string;
}
I think that the CString object that is created in this function would be in stack and may lose it value once goes out of this function.
In case of Objective-C code, we can retain the autoreleased object to use it. Is there any such mechanism available in C++?
In C++ you can do
and delete (by calling delete on the pointer) the string in the caller after he is done with it if you want to have the string on the heap. However in the first version you posted, I see no problem. It is a stack variable, but of course it is still valid in the caller as it is the return value.