首页 \ 问答 \ 带有可选参数的c ++函数(c++ function with optional arguments)

带有可选参数的c ++函数(c++ function with optional arguments)

声明这样的方法的最佳方法是什么:

calc( string name, string arguments, 
      string lib, 
      string &output, string &error, 
      string finishInfo )

每个参数都是可选的? 如果需要,用户可以传递名称,参数和错误; 只传递finishInfo或传递所有六个参数。 可能是最友好的用户可以打电话:

calc( "my name", , "my library", , , );

但我不知道是否有可能没有提升。 我不能超载,因为我需要创建62种方法。 我最好的想法是:

calc( string * name = NULL, string * arguments = NULL, 
      string * lib = NULL, 
      string * output = NULL, string * error = NULL, 
      string * finishInfo = NULL )

我将所有内容都改为指针,这样我们就需要调用

calc( ptr_myName, NULL, ptr_myLib, NULL, NULL, NULL );

要么

calc( string name = "", string arguments = "",
      string lib = "",
...
calc( "", "",
      "my library",

What is the best way to declare a method like this:

calc( string name, string arguments, 
      string lib, 
      string &output, string &error, 
      string finishInfo )

where every parameter is optional? User can pass name, arguments and error if needed; pass only finishInfo or pass all six arguments. Probably the most user friendly is being able to call:

calc( "my name", , "my library", , , );

but I don't know if it's possible to do without boost. I cannot overload, because I'd need to create 62 methods. My best idea is:

calc( string * name = NULL, string * arguments = NULL, 
      string * lib = NULL, 
      string * output = NULL, string * error = NULL, 
      string * finishInfo = NULL )

I'm changing everything to pointers, this way we need to call

calc( ptr_myName, NULL, ptr_myLib, NULL, NULL, NULL );

or

calc( string name = "", string arguments = "",
      string lib = "",
...
calc( "", "",
      "my library",

原文:https://stackoverflow.com/questions/41833458
更新时间:2022-09-22 18:09

最满意答案

我会给你一个快速而肮脏的答案,理想情况下你会使用一个框架,允许你实例化一个类并将其注入你拥有的任何控制器中(我假设你没有使用框架,因为你没有'在你的问题上提及任何事情)

但是去准系统j2ee(好软件的神,请原谅我!)

  • 在您的servlet上,您将在doPostdoGet方法上拥有一个HttpServletRequest实例。
  • 调用HttpServletRequest.getServletContext ,它将返回一个ServletContext实例,您可以将其用作存储所有类型事物的映射。 此实例在转到您的应用程序的所有请求之间共享。
  • 调用ServletContext.getAttribute(name)来获取你的计数器类(如果你有很多访问者,我会建议像AtomicLong这样的东西:D)。

I'll give you a quick and dirty answer as ideally you would be using a framework that allows you to instantiate a class and inject it in whatever controllers you have (I'm assuming you're not using a framework, as you didn't mention anything on your question)

But going to barebones j2ee (gods of good software, please forgive me!)

  • On your servlet, you'll have an instance of a HttpServletRequest on your doPost or doGet method.
  • Call HttpServletRequest.getServletContext, that will return an ServletContext instance that you can use as a map to store all sort of things. This instance is shared amongst all the requests that go to your application.
  • Call ServletContext.getAttribute(name) to get your counter class (I would suggest something like an AtomicLong in case you have lots of visitors :D).

相关问答

更多
  • 首先,在q中设置最小/最大运行时间参数不会阻止perm-gen错误......你可能需要增加内存(取决于应用程序的占用空间),因为你看到了那些ESP。 如果您将最小/最大值设置得更高。 其次 - 它肯定会限制并发性但不是那么多 - 将其视为mongrel(瘦)集群:并发请求处理> =正在运行的实例数 单个运行时在单个请求的持续时间内被阻塞(完全不依赖于任何会话状态),在返回响应后,运行时将返回到池并准备好处理另一个请求。 如果请求进入并且池中没有运行时,它将尝试在返回HTTP错误之前等待一个可用(可以使用j ...
  • 我在stackoverflow上每天都会看到很多NoClassDefFoundError。 所以决定为它写一篇文章。 你应该能够在抛出这个之后解决问题: http://javareferencegv.blogspot.com/2013/10/debugging-javalangnoclassdeffounderror.html 在您的情况下,未加载org.hibernate.service.jndi.JndiNameException 。 它必须在hibernate-core jar中 I see a lo ...
  • 我一周前遇到过这个问题(在Ubuntu 11.04上)。 我没有找到为什么它不在视图中显示服务器 - 它们出现在Servers项目中。 但我通过切换到一个新的工作区来修复它。 (当然,假设您在服务器视图中右键单击并选择“新建 - >服务器”) I had this problem a week ago (on Ubuntu 11.04). I failed to find why it doesn't show the servers in the view - they appear in the Ser ...
  • 编辑 事实证明,当你启动tomcat服务器时(或之后),而不是在部署应用程序时,你要传递运行时参数。 所需的命令是: export CATALINA_OPTS="-DconfigLocation=/usr/share/tomcat5/.grails/TAPaS/" 或者你想要的任何参数/值。 这可以在服务器启动后手动输入,也可以在CATALINA_HOME目录中创建包含该命令的CATALINA_HOME文件。 这可以使用echo $CATALINA_HOME 。 找到它的另一种方法是使用下面的启动命令,并 ...
  • 有趣的事情...... 我通常使用Reflection和运行时数据的组合记录所有方法参数名称和值,从未考虑过捕获局部变量值... 这里有一些信息: 访问本地变量信息 Interesting thing... I usually log all method parameters names and values with a combination of Reflection and runtime data, never thought about also catching local variable ...
  • 您可能需要查看此解决方案或查看maven货运插件 。 第一个解决方案使用了sysdeo tomcat插件,我个人过去使用过这个插件,效果很好。 它不会为你下载tomcat。 您必须手动执行此操作。 现在我正在使用maven货运插件(虽然不是eclipse插件),它将为您的开发目的下载一个tomcat实例。 You might want to take a look at this solution or check out the maven cargo plugin. The first solution ...
  • 我会给你一个快速而肮脏的答案,理想情况下你会使用一个框架,允许你实例化一个类并将其注入你拥有的任何控制器中(我假设你没有使用框架,因为你没有'在你的问题上提及任何事情) 但是去准系统j2ee(好软件的神,请原谅我!) 在您的servlet上,您将在doPost或doGet方法上拥有一个HttpServletRequest实例。 调用HttpServletRequest.getServletContext ,它将返回一个ServletContext实例,您可以将其用作存储所有类型事物的映射。 此实例在转到您的 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)