简体   繁体   English

是否存在与Objective-C自动释放相当的C ++?

[英]Is there a C++ equivalent of Objective-C's autorelease?

I can write a function this way in Objective-C. 我可以在Objective-C中以这种方式编写一个函数。 This can be used to churn out many UIButtons. 这可以用来制作许多UIButton。

+(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++? 可以用C ++完成吗? I am not asking about creation of UIButton in C++. 我不是要求在C ++中创建UIButton。

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. 我认为在此函数中创建的CString对象将在堆栈​​中,并且一旦离开此函数可能会丢失它的值。

In case of Objective-C code, we can retain the autoreleased object to use it. 在Objective-C代码的情况下,我们可以保留自动释放的对象来使用它。 Is there any such mechanism available in C++? C ++中是否有这样的机制?

In C++ you can do 在C ++中你可以做到

CString* getCString(some parameters)
{

    CString* string = new CString(L"Hi sample string.");
    return string;

 }

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. 如果你想在堆上有字符串,那么在完成后调用者中的字符串并删除(通过调用指针上的delete)。 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. 它是一个堆栈变量,但当然它在调用者中仍然有效,因为它是返回值。

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. 我认为在此函数中创建的CString对象将在堆栈​​中,并且一旦离开此函数可能会丢失它的值。

It doesn't lose its value since it will be copied (although the compiler can elide the copy as an optimization). 它不会丢失它的值,因为它将被复制(尽管编译器可以将副本作为优化来删除)。

So don't worry – this works just fine in C++. 所以不要担心 - 这在C ++中运行得很好。

You can also return a pointer to dynamically allocated memory but this makes usage harder since the client has to free the memory explicitly. 您还可以返回指向动态分配内存的指针,但由于客户端必须明确释放内存,因此这会使使用更加困难。 In general this is not a good solution – handling raw pointers to dynamic memory is the #1 reason for memory leaks and cleaning such code up is horrible. 一般来说,这不是一个好的解决方案 - 处理动态内存的原始指针是内存泄漏的首要原因,清理此类代码非常糟糕。

A better way is to use smart pointers (eg shared_ptr or unique_ptr ) that take care of the memory management themselves. 更好的方法是使用智能指针(例如shared_ptrunique_ptr )来自己处理内存管理。

Your string variable will eventually go out of scope, but return string; 你的string变量最终将超出范围,但return string; will return only the copy and hence you can use it in C++ as such now... 将仅返回副本 ,因此您现在可以在C ++中使用它...

Your Obj-C code works against coding styles and memory management rules. 您的Obj-C代码适用于编码样式和内存管理规则。 It should look like: 它应该看起来像:

+(UIButton*)buttonWithFrame:(CGRect)aRect {
  UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  [button setTitle:@"Some title" forState:UIControlStateNormal];
  button.frame = aRect;
  return button;
}

Seems like minor changes, but it makes memory managament (object ownership) obvious and names the parameter appropriately. 看似微小的变化,但它使记忆管理(对象所有权)显而易见,并恰当地命名参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM