假设我在C++中有一个函数,我在其中使用指向其内存地址的指针来调用它typedef.现在,我怎样才能在Delphi中做同样的事情?
例如:
typedef void (*function_t)(char *format, ...);
function_t Function;
Function = (function_t)0x00477123;
Run Code Online (Sandbox Code Playgroud)
然后,我可以用:Function("string", etc);.
在Delphi中,有没有办法在不使用汇编指令的情况下执行此操作?
请注意,它是一个可变参数函数.
在下面的程序中,我有一个函数重载.一个只有一个参数,另一个有两个参数,另一个有三个.在下面的示例中,它看起来很简单,因为函数不会太长.如果函数很长并且使用不同的输入参数一次又一次地编写相同的函数看起来很难看.一种方法可以做到这一点variadic functions.如果我知道我的函数只需要1,2或3个输入参数是否variadic functions真的有必要?如果是这样我怎么能这样做?注意:具有三个输入参数和两个输入参数的函数执行不同的计算.
#include <iostream>
using namespace std;
int function(int a, int b, int c) // All the arguments are always of the same type
{
return a*b*c;
}
int function(int a, int b)
{
int c = a; // Always duplicate the first argument
return a*b*c;
}
int function(int a)
{
int b = a, c = a; // Always duplicate the first argument
return a*b*c;
}
int main()
{
cout<<function(2,3,4)<<"\n"<<function(2,3)<<"\n"<<function(2);
cin.ignore();
return 0;
} …Run Code Online (Sandbox Code Playgroud) 是否可以使用va_list在C中定义结构?就像是:
struct node
{ int value;
va_list children;
};
Run Code Online (Sandbox Code Playgroud) 变量函数是否已被弃用,因为C++ 11具有可变参数模板函数?更具体地说,变量函数在特定情况下有任何优势吗?我知道variadic-macro可以有它们的用途.可变函数怎么样?
在Java中是否可以创建类似于此execute方法的方法:
public void execute(Runnable... mrs) {
for (Runnable mr : mrs) {
mr.run();
// should print some expected string here
}
}
public void sleep(int seconds) {
try {
Thread.sleep(1000 * seconds);
} catch (InterruptedException ex) {
log.error("Error occured", ex);
}
}
public void customExecute() {
execute(() -> sleep(1), () -> sleep(2));
}
Run Code Online (Sandbox Code Playgroud)
但在执行每个操作后,它应该打印传递的String参数和(问题的关键时刻)它应该支持一行使用 - 类似于:
execute(("Action 1 passed") -> someAction(), ("Other action passed") -> otherAction());
Run Code Online (Sandbox Code Playgroud) 我将列表传递给一个接受多个参数(*值)的方法.如果我传递多个由","分隔的值,那么一切都很好,但如果我传递一个列表*值变成一个元组并且不迭代这些值,唯一的元素就是我要迭代的列表.有人可以解释一下吗?有没有办法解决这个问题,两种方式都有效?
我的方法:
def accept(*values):
for value in values:
#do something with value
Run Code Online (Sandbox Code Playgroud)
作品:
foo.accept(value1, value2, value3)
Run Code Online (Sandbox Code Playgroud)
不起作用:
values = [value1, value2, value3]
foo.accept(values)
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助
以下max函数应该返回5,但它返回4294967294.我怀疑这种奇怪的行为是由于铸造变量引起的,但却无法弄清楚.有人可以检测到故障吗?
系统:Windows 7(64位),mingw64
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <inttypes.h>
int64_t max(int64_t n, ...) {
va_list v;
int64_t i, t, max = INT64_MIN;
va_start(v, n);
for (i = 0; i < n; i++) {
t = va_arg(v, int64_t);
if (max < t) {
max = t;
}
}
va_end(v);
return (max);
}
int main(int argc, char **argv) {
printf("max(3, 1, 5, -2) : %3I64d\n", max(3, 1, 5, -2));
return (0);
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在不使用lua内的unpack的情况下将数字顺序表作为vararg返回?换一种说法; 你能重新打开解包功能吗?
示例表:
foo = {1, 2, 3}
Run Code Online (Sandbox Code Playgroud)
功能示例:
function unpackTable( tab )
--
end
Run Code Online (Sandbox Code Playgroud)
要求的结果:
1, 2, 3
Run Code Online (Sandbox Code Playgroud) 请考虑下表:
julia> using RDatasets, DataFrames
julia> anscombe = dataset("datasets","anscombe")
11x8 DataFrame
| Row | X1 | X2 | X3 | X4 | Y1 | Y2 | Y3 | Y4 |
|-----|----|----|----|----|-------|------|-------|------|
| 1 | 10 | 10 | 10 | 8 | 8.04 | 9.14 | 7.46 | 6.58 |
| 2 | 8 | 8 | 8 | 8 | 6.95 | 8.14 | 6.77 | 5.76 |
| 3 | 13 | 13 | 13 | 8 | …Run Code Online (Sandbox Code Playgroud) 有没有办法在可变参数函数中std ::转发特定范围的参数?例如:
#include <iostream>
template<typename T>
void test_simple(T v0,T v1)
{
std::cout<<v0<<","<<v1<<std::endl;
}
template<typename... TARGS>
void test_variadic(TARGS ...args)
{
test_simple(std::forward<TARGS>(args)...); // Forward all arguments except for the first one?
}
int main(int argc,char *argv[])
{
test_variadic(5.f,2.f,7.f);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我希望test_variadic只将最后两个参数转发给test_simple,这样输出就是"2.0,7.0".