带有可选参数的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
最满意答案
我会给你一个快速而肮脏的答案,理想情况下你会使用一个框架,允许你实例化一个类并将其注入你拥有的任何控制器中(我假设你没有使用框架,因为你没有'在你的问题上提及任何事情)
但是去准系统j2ee(好软件的神,请原谅我!)
- 在您的servlet上,您将在
doPost
或doGet
方法上拥有一个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 yourdoPost
ordoGet
method.- Call
HttpServletRequest.getServletContext
, that will return anServletContext
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).
相关问答
更多-
除了IIS和Apache,还有没有其它的服务器程序[2022-05-09]
tomcat -
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
jruby运行时和tomcat(jruby runtimes and tomcat)[2021-12-21]
首先,在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 ...
-
Eclipse Servers视图未显示添加的Tomcat运行时环境(Eclipse Servers view not showing added Tomcat runtime environment)[2022-02-23]
我一周前遇到过这个问题(在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实例,您可以将其用作存储所有类型事物的映射。 此实例在转到您的 ...