首页 \ 问答 \ 在哪里分配一次使用类?(Where to allocate one time use class?)

在哪里分配一次使用类?(Where to allocate one time use class?)

让我们考虑以下代码:

void main(int argc, char* argv[])
{
Foo foo;
//at this point I don't need foo any more
//a lot of stuff here
}

如果我只需要foo很短的时间,那么在执行其余代码之前将它分配到堆并删除是不是更好?


Lets consider the following code:

void main(int argc, char* argv[])
{
Foo foo;
//at this point I don't need foo any more
//a lot of stuff here
}

If I only need foo only for short amount of time,isn't it would be better to allocate it on a heap and delete before executing rest of the code?


原文:https://stackoverflow.com/questions/16101355
更新时间:2019-12-13 07:41

最满意答案

不,最好写一个内部范围

int main()
{
    {
        Foo foo;
        // use foo
    }
    // more code
}

但这样做应该暗示将foo放在一个完全独立的函数中可能更好。

这里没有理由使用堆分配。 那个解决方案会比问题更糟糕。


No, it's better to write an inner scope.

int main()
{
    {
        Foo foo;
        // use foo
    }
    // more code
}

But doing this should be a hint that it might be better to put foo in a completely separate function.

There's no reason to use heap allocation here though. That solution would be worse than the problem.

2013-04-19

相关问答

更多

Rails 4 InvalidAuthenticityToken(Rails 4 InvalidAuthenticityToken)

正如您所说,这可能是由于会话在用户在很久以前打开选项卡上触发AJAX请求之前到期。 在Rails 4中,处理未验证请求的默认策略是引发InvalidAuthenticityToken错误: 来自/app/controllers/application_controller.rb: class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # Fo

使用带脚本的setuptools进行Python相对导入(Python relative imports using setuptools with scripts)

该问题与setuptools 。 在正在执行的模块中使用相对导入作为__main__不能立即使用。 有解决方法/黑客,但最常见的解决方案似乎是将脚本从包中移出或在脚本文件中使用绝对导入。 有关完整故事,请查看Python 3中的相对导入 。 The problem is not related with setuptools. Using relative imports inside the module being executed as __main__ does not work out

带有overflow-x的移动导航:隐藏但左右可滚动(参见示例)(Mobile nav with overflow-x:hidden but still scrollable left and right (See example))

CSS overflow-y属性指定元素内容溢出时顶部/底部边缘的效果。 参考: http : //www.w3schools.com/cssref/css3_pr_overflow-y.asp 对于左/右边缘,使用overflow-x属性。 所以在导航栏中overflow-y: hidden; property指定顶部/底部边缘应隐藏内容。 这不会影响左/右滚动边缘。 通常,如果要创建垂直滚动元素,则可以执行以下操作: #con { width: 300px; overflow-x

如何使用jquery应用自定义别名输入掩码?(How do I apply a custom alias inputmask using jquery?)

我查看了jquery.inputmask 3.x的文档。根据我的理解,更改别名属性的首选方法是创建一个继承自默认别名定义的新别名。 例 Inputmask.extendAliases({ 'numeric': { "prefix":"Rp", "digits":0, "digitsOptional":false, "decimalProtect":true, "groupSeparator":",",

可编程Xbee模块的自动配置(Autoconfiguration on programmable Xbee modules)

是的,开发套件包括一个API,用于从协处理器向这些板上的无线电发送AT命令。 还有一种直通模式,可将主机串行端口中继到无线电处理器,这有助于模块的初始设置/配置,就像您在制造过程中所做的那样。 Yes, the development kit includes an API for sending AT commands from the co-processor to the radio on those boards. There's also a passthrough mode that

SSH上的简单计划任务(Simple Scheduled Task on SSH)

你知道crontab吗? 您可以随时创建计划任务,例如: # m h dom mon dow user command 1 2,14 * * * user restart Do you know crontab? You can create there scheduled tasks at any time, for example: # m h dom mon dow user command 1 2,14 * * * user restart

将日期转换为时间戳(converting date into timestamp)

'25. 02. 2014 18:48:21' '25. 02. 2014 18:48:21'不是有效的日期格式。 你必须先用正则表达式转换它,就像那样: var someDate = '25. 02. 2014 18:48:21'; var converted = someDate.replace(/^(\d{2}\. )(\d{2}\. )(\d{4})/, '$3. $2$1'); // converted is in format: YYYY. MM. DD. var timestamp

本地端口从Mac转发到Linux用于Django服务器(Local port forwarding from Mac to Linux for Django server)

正如道格拉斯指出的那样,Mac上的端口5900可能已经被使用了。 在我的愚蠢中,我曾经认为还有其他事情发生,因为我之前尝试过5901时发生了同样的事情。 我甚至没有考虑两者都被使用的可能性。 去像5903这样的实际未使用的端口工作得很好。 As Douglas pointed out, port 5900 on my Mac was probably already being used. In my silliness I had thought that there was somethin

相关文章

更多

最新问答

更多
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • 在main方法中进行更改后传递给构造函数的TreeMap实例的行为是什么(What is the behavior of a TreeMap instance passed into a constructor following changes in main method)
  • 在这个的一些属性不同的颜色(Different color in some properties of this)
  • CURL没有返回任何内容(CURL Not returning anything)
  • MVC5注入依赖于城堡windsor的视图(MVC5 injecting dependency on a view with castle windsor)
  • CakePHP AJAX-Call:发生错误:未定义(CakePHP AJAX-Call: An error occured: undefined)
  • 如何用.aidl扩展名创建文件?(how to create a file with .aidl extension?)
  • 无法获得全尺寸动态WordPress灯箱图像显示(Cannot get full size dynamic WordPress lightbox image to display)
  • 使用Jsoup删除元素不起作用(Removing Element with Jsoup doesn't work)
  • 交叉编译ARM的MongoDB C ++驱动程序。(Cross compiling MongoDB C++ driver for ARM. Cannot find Boost Libraries)
  • ProgressDialog没有关闭(ProgressDialog not closing)
  • 单元测试DB中的JPA插入(Unit testing JPA insertion in the DB)
  • 点击谷歌服务对话框中的更新按钮(Click on update button in check google service dialog)
  • 适用于PhoneGap Build的iOS“缺少必需的57x57图标”(“Missing required 57x57 icon” for iOS on PhoneGap Build)
  • MVC ckeditor post编辑器值内联(MVC ckeditor post editor value inline)
  • Angular 2 Pipe - 无法读取未定义的属性'toString'(Angular 2 Pipe - Cannot read property 'toString' of undefined)
  • 安装引导加载程序(Installing bootloaders)
  • 用于确定最终如何包含头文件的工具(Tool to figure out how a header file is eventually being included)
  • 在将字符串转换为int [duplicate]之前检查字符串是否不是数字(Check if string is not a number before converting it to a int [duplicate])
  • 从脚本构建db2数据库以进行构建验证的工具?(Tools to build db2 database from scripts for build verification?)
  • 在偏移之后向FileOutputStream写一个数字(Write a number to FileOutputStream after an offset)
  • Javascript RegEx仅限数字(无特殊字符)(Javascript RegEx for Numbers Only (no special characters))
  • 我可以在PHP的Linux服务器上运行新的COM(“WScript.Shell”)(Can I run new COM(“WScript.Shell”) on linux servers in PHP)
  • 在Backbone.js中放置与视图不直接相关的代码(例如广告,分析等)的位置?(Where to place code not directly related to a view (such as ads, analytics etc) in Backbone.js?)
  • 适用于Mac和PC的一般开发人员必备软件综合列表[关闭](Comprehensive List of Essential Software for General Developers on Mac and PC [closed])
  • 将语言文本转换为模型(Transformation Language Text to Model)
  • 在控制器中的两个方法之间共享变量?(Sharing variables between two methods in a controller?)