首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

int *(*a[5])(int, char*); 请解析一上,看到这种带括号的就头疼

2013-01-08 
int *(*a[5])(int, char*)请解析一下,看到这种带括号的就头疼1、int *(*a[5])(int, char*)//这条语句声明

int *(*a[5])(int, char*); 请解析一下,看到这种带括号的就头疼
1、int *(*a[5])(int, char*);  //这条语句声明的是什么
2、class Interface
{
    int member;
};
class Base {};
class Derived : public Interface, public Base {};
int main()
{
    Base* b = (Base*)100;//这行是什么意思
    Derived* d1 = reinterpret_cast<Derived*>( b );
    Derived* d2 = static_cast<Derived*>( b );
}

请解析一下,谢谢,看到这种带括号的就头疼
[解决办法]
int* (*a[5])(int,char*) 

int* 是返回类型
(*a[5]) 是函数名(指针数组函数)
[解决办法]
第一个问题:
int (*a[5])(int, char*);  代表定义一个a数组,里面包含了5个函数指针
int *(*a[5])(int, char*); 代表取a[0]的函数指针

第二个问题
class Interface
{
    int member;
};
class Base {};
class Derived : public Interface, public Base {};
Base,因为没虚函数,所以没虚表,那么对象内存里面只有member。
所以
Base* b = (Base*)100;

Base* b = new Base;
b->member = 100
等价的
[解决办法]
int *(*a[5])(int, char*);  
拆开来分析:
typedef int* (*f)(int,char*);
f a[5];
所以a是个5个元素的数组,每个元素为函数指针.
[解决办法]

引用:
int *(*a[5])(int, char*);  
拆开来分析:
typedef int* (*f)(int,char*);
f a[5];
所以a是个5个元素的数组,每个元素为函数指针.


用代码可以验证。

#include <stdio.h>
#include <stdlib.h>

int* (*a[5])(int,char*);

int *foo(int n, char *s)
{
    int *p;
    
    p = malloc(sizeof(int));
    *p = n + atoi(s);

    return p;
}

int main(int argc, char *argv[])
{
    int *p;

    a[0] = &foo;
    p = (*a[0])(1, "2");

    printf("%d\n", *p);

    return 0;
}


[解决办法]
1、int *(*a[5])(int, char*);  //这条语句声明的是什么
2、class Interface
{
    int member;
};
class Base {};
class Derived : public Interface, public Base {};
int main()
{
    Base* b = (Base*)100;//这行是什么意思
    Derived* d1 = reinterpret_cast<Derived*>( b );
    Derived* d2 = static_cast<Derived*>( b );
}

这样看第一个,第一题首先显然是一个函数指针数组,而且函数的返回值是int*,有两个参数。
出现了a[5]说明一定是数组,即a是函数指针数组。

二,
   就是把地址100转换成Base类型指针

热点排行