[问题] 函数指标的void*参数个数可不定?

楼主: aMaa (aMa)   2015-03-16 18:48:39
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
我看了别人的程式,当中他宣告一个function pointer, 其中引数部份有六个void*,
这个function pointer可以接受不同的回传值型别或引数个数不定的function address,
我用VC++验証如下:
// void_ptr.cpp : main project file.
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef bool (*func_ptr)(void* input0, void* input1, void* input2, void* input3, void* input4, void* input5, void* input6);
bool func1(int* a, int* b){
cout << __FUNCTION__;
return true;
}
bool func2(char* a, int* b, int* c){
cout << __FUNCTION__ ;
return true;
}
bool func3(int* a, int* b, char* c, char* d){
cout << __FUNCTION__;
return true;
}
void func4(int* a, int* b)
{
cout << __FUNCTION__;
}
void func5(void)
{
cout << __FUNCTION__;
}
static func_ptr list[]={
(func_ptr)&func1,
(func_ptr)&func2,
(func_ptr)&func3,
(func_ptr)&func4,
(func_ptr)&func5,
};
int main(array<System::String ^> ^args)
{
void *g0, *g1, *g2,*g3, *g4, *g5, *g6;
int x=10;
int y=5;
g0=&x;
g1=&y;
list[0](g0,g1,g2,g3,g4,g5,g6);
char* str;
str="hihihi";
g0=str;
g2=&y;
list[1](g0,g1,g2,g3,g4,g5,g6);
list[4](g0,g1,g2,g3,g4,g5,g6);
system("pause");
return 0;
}
结果会印出func1func2func5,无任何错误
我一直以为function pointer只能接受与宣告时相同型别的
function address(回传值,引数型别,引数个数),
别人的code是在linux下, 我的验証是在vs2010 vc++,
这该如何解释, void*可以接受任何位址, 但在转成(func_ptr)时应该mapping错才对...
喂入的资料(Input):
预期的正确结果(Expected Output):
错误结果(Wrong Output):
程式码(Code):(请善用置底文网页, 记得排版)
补充说明(Supplement):
作者: Killercat (杀人猫™)   2015-03-16 18:51:00
Koenig Lookup?ar...你问的应该不是这个 请忽略 XD
作者: purpose (秀才遇到肥宅兵)   2015-03-16 19:25:00
都是一群指标传来传去,当然没事,把 func1 的形式参数 a改成 double,就会有冲突,就会跟 func_ptr 形参有冲突然后虽然 list[1] 的参数个数,跟实际指向的函数需要的不同,可是堆叠的控制是 cdecl,所以不会损坏程式
作者: scwg ( )   2015-03-16 23:01:00
Undefined behavior, 只是 vc 刚好选择直觉的输出http://stackoverflow.com/questions/559581

Links booklink

Contact Us: admin [ a t ] ucptt.com