ch3-ComplDefs (Complicated declarations and definitions)

Chapter_3     Exercise_3-31     Assert PointerToFunction     Exercise_3-32







ComplDefs.cpp     TCP1, p. 214         download


// Complicated declarations and definitions:
typedef void* (*vpa)[10];
vpa f1(int i);
void * (*(*fp1)(int))[10];

typedef float (*f21)(int);
f21 f23(int,int,float);
float (*(*fp2)(int,int,float))(int);

typedef double (*f3)();
typedef f3 (*a3)[10];
a3 fa3(); // function declaration
typedef double (*(*(*fp3)())[10])();
fp3 a = &fa3; // fp3 a = fa3; // global variable a (pointer to function)
a3 aa = (*a)();
a3 aaa = a();

typedef int (*fp4)();
typedef fp4 (*a4)[10];
int (*(*f4())[10])();

int main()
{
vpa a; // local variable a (pointer to array)
fp1 = &f1; // fp1 = f1;
a = (*fp1)(0);
a = fp1(0);
a = (*f1)(0);
a = f1(0);

fp2 = &f23; // fp2 = f23;
(*fp2)(0,0,0);
fp2(1,2,3);
(*f23)(0,0,0);
f23(1,2,3);

(*::a)(); // global a (pointer to function)
::a(); // `::' accesses the global namespace

(*f4)();
f4();

return 0;
}
// Function definitions:
vpa f1(int i)
{
vpa a;

return a;
}

f21 f23(int i, int j, float f)
{
f21 a;

return a;
}

a3 fa3()
{
a3 a;

return a;
}

a4 f4()
{
a4 a;

return a;
}
/*
g++ ComplDefs.cpp -o ComplDefs
./ComplDefs
*/





Notes:  See Namespaces on CPP_Reference for accessing the global namespace with the scope_resolution_operator `::'. See also Exercise_3-33.









Chapter_3     Exercise_3-31     Assert BACK_TO_TOP PointerToFunction     Exercise_3-32



Comments

Popular posts from this blog

Contents