我正在用PHP编写一个构造,其中解析器确定动态调用哪个函数,类似这样:
// The definition of what to call
$function_call_spec = array( "prototype" => "myFunction",
"parameters" => array( "first_par" => "Hello",
"second_par" => "World"));
// Dispatch
$funcPrototype = $function_call_spec["prototype"];
$funcPrototype(); // Here we call function 'myFunction'.
Run Code Online (Sandbox Code Playgroud)
这一切都很好,花花公子.但现在是下一步,传递参数,我不知道是否有可能按照我想要的方式进行.它永远不会让我惊讶,但是这些天脚本语言可以做什么,所以这里是:
可以将参数传递给函数,如下所示:
// Here we call function 'myFunction' with the array of parameters.
$funcPrototype( $function_call_spec["parameters"] );
Run Code Online (Sandbox Code Playgroud)
但是,我想用明确的参数等正确地声明'myFunction':
function myFunction( $first_par, $second_par )
{
}
Run Code Online (Sandbox Code Playgroud)
接下来的问题是 - 有没有办法通过循环参数数组动态地将参数传递给函数?
为了澄清,我并不想这样做,是这样的:
$funcPrototype( $function_call_spec["parameters"]["first_par"],
$function_call_spec["parameters"]["second_par"] );
Run Code Online (Sandbox Code Playgroud)
因为这需要我的代码静态地知道关于myFunction的细节,这违背了整个想法.
相反,我想以某种方式这样做:
// Special magic PHP function which can be used …Run Code Online (Sandbox Code Playgroud) 这应该是一个快速的.有可能做这样的事情:
[Callback(funtionY)]
void functionX()
{
}
void functionY()
{
}
Run Code Online (Sandbox Code Playgroud)
那么当调用functionX时,也会自动调用functionY?我问的原因是因为我要为基于XNA的小型游戏引擎实现网络功能,我想将函数标记为Synched,以标记在调用函数时应该在所有客户端调用它.
谢谢.
如果我有这个:
class A:
def callFunction(self, obj):
obj.otherFunction()
class B:
def callFunction(self, obj):
obj.otherFunction()
class C:
def otherFunction(self):
# here I wan't to have acces to the instance of A or B who call me.
...
# in main or other object (not matter where)
a = A()
b = B()
c = C()
a.callFunction(c) # How 'c' know that is called by an instance of A...
b.callFunction(c) # ... or B
Run Code Online (Sandbox Code Playgroud)
尽管存在设计或其他问题,但这只是一个探究性思维的问题.
注意:必须在不更改 签名的情况下完成此操作otherFunction
执行这个简单的代码:
int foo(int* a){
cout <<"a="<<a;
*a=1;
cout <<", *a="<<*a<<endl;
return 0;}
int main () {
int* ptr;
ptr=new int[2];
ptr[0]=0;
ptr[1]=0;
cout<< foo(ptr) <<" "<< ptr <<" *ptr="<< *ptr <<endl;
cout<< foo(ptr) <<" "<< ptr <<" *ptr="<< *ptr <<endl;
return 0;}
Run Code Online (Sandbox Code Playgroud)
导致(linux):
a=0x939f008, *a=1
0 0x939f008 *ptr=0
a=0x939f008, *a=1
0 0x939f008 *ptr=1
Run Code Online (Sandbox Code Playgroud)
请解释为什么*ptr = 0在第二行,但不在第四行; 可能是,"东西" cout从右到左被"取出" ?比 - 它如何真正起作用(在运行时一步一步)?
c++ pointers side-effects function-calls operator-precedence
我正在Python控制台上试验一些东西,我注意到函数名称和()Python 之间有多少空格并不重要,Python仍设法调用该方法?
>>> def foo():
... print 'hello'
...
>>> foo ()
hello
>>> foo ()
hello
Run Code Online (Sandbox Code Playgroud)
怎么可能?不应该引发某种例外吗?
我想使用的功能来自Bioconductor的包hypergraph,并hyperdraw没有装载包.从小hyperdraw插图运行示例时
dh1 <- hypergraph::DirectedHyperedge("A", "B", "R1")
dh2 <- hypergraph::DirectedHyperedge(c("A", "B"), c("C", "D"), "R2")
hg <- hypergraph::Hypergraph(LETTERS[1:5], list(dh1, dh2))
hgbph <- hyperdraw::graphBPH(hg)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Error in hyperdraw::graphBPH(hg) : could not find function "hyperedges"
Run Code Online (Sandbox Code Playgroud)
如果我尝试加载hyperedges:
hyperedges <- hyperdraw:::hyperedges
Run Code Online (Sandbox Code Playgroud)
我收到了错误
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'hyperedges' not found
Run Code Online (Sandbox Code Playgroud)
当我使用library或加载两个包时require,我没有得到任何错误(在运行上面的代码时没有hypergraph::和hyperdraw::).
为什么我不希望加载包的原因是因为我建立它采用了封装hyperdraw和hypergraph只有一个功能,我宁愿把这些包装成Suggests比到Depends我的DESCRPTION文件.
有谁知道如何解决这个问题?
我的问题最容易解释为代码块和它下面的问题.
但这是试图用英语解释我的问题:
我正在学习JavaScript(而且我很懒,所以我使用了很多jQuery ...;))我遇到了一些我不太了解的东西,我不知道它叫什么,所以我不喜欢我知道应该研究什么术语.
基本上我想知道何时需要使用function(){ ... }(没有名称)以及何时使用function function_name(){ ... }(带有名称),以及如何使用.click() .post()或等执行函数setTimeout().
我知道一些jQuery函数,比如.click()需要你输入一个函数来调用.但问题是,你不能只说.click( function_name() )(如果我正确地解释了我的测试结果,这将在执行脚本时立即调用该函数)而不是你必须做.click( function(){ ... })或你可以做.click( function function_name(){ ... }).我还发现.click( function_name )只要事先定义就可以说,var function_name = function function_name(){ ... }而函数名是可选的,无论是否添加它都可以.
我举了一个例子,说明了我能想到的所有可能情况.我确实找到了什么工作,什么不工作,现在我想找出为什么每个人都在工作而其他人没有工作.
我希望理解这将有助于我更好地理解异步函数,如.post()或setTimeout().
<button id="one">Button 1</button>
<button id="two">Button 2</button>
<button id="three">Button 3</button>
<button id="four">Button 4</button>
<button id="five">Button 5</button>
<button id="six">Button 6</button>
<button id="seven">Button 7</button>
<button id="eight">Button 8</button>
<button …Run Code Online (Sandbox Code Playgroud) 在下面的表达式中是否有一些确定的函数调用顺序,或者它是否因编译器而异?
此处的规则是否适用 - 在C中,未指定大多数运算符的函数和操作数的参数的计数顺序.在此Wiki页面中找到了上述规则
a = (f1(10, 20) * f2(30, 40)) + f3()
Run Code Online (Sandbox Code Playgroud) 我没有装配经验,但这是我一直在努力的.如果我缺少传递参数和通过程序集中的指针调用函数的任何基本方面,我想输入.
举例来说,如果我应该恢复我想知道ecx,edx,esi,edi.我读到它们是通用寄存器,但我找不到它们是否需要恢复?打电话后我应该做什么样的清理工作?
这是我现在的代码,它确实有效:
#include "stdio.h"
void foo(int a, int b, int c, int d)
{
printf("values = %d and %d and %d and %d\r\n", a, b, c, d);
}
int main()
{
int a=3,b=6,c=9,d=12;
__asm__(
"mov %3, %%ecx;"
"mov %2, %%edx;"
"mov %1, %%esi;"
"mov %0, %%edi;"
"call %4;"
:
: "g"(a), "g"(b), "g"(c), "g"(d), "a"(foo)
);
}
Run Code Online (Sandbox Code Playgroud) 以下表达式在haskell中的含义是什么?
($ 3)
Run Code Online (Sandbox Code Playgroud)
ghci显示以下类型
($ 3) :: Num a => (a -> b) -> b.
Run Code Online (Sandbox Code Playgroud) function-calls ×10
c ×2
function ×2
python ×2
assembly ×1
attributes ×1
bioconductor ×1
c# ×1
c++ ×1
haskell ×1
javascript ×1
oop ×1
operators ×1
package ×1
php ×1
pointers ×1
python-2.7 ×1
r ×1
side-effects ×1
syntax ×1
x86 ×1