int **p1; // p1 is a pointer to a pointer to an int.
int *&p2; // p2 is a reference to a pointer to an int.
int &*p3; // ERROR: Pointer to a reference is illegal.
int &&p4; // ERROR: Reference to a reference is illegal.
char ** p1; // pointer to pointer to char
const char **p2; // pointer to pointer to const char
char * const * p3; // pointer to const pointer to char
const char * const * p4; // pointer to const pointer to const char
char ** const p5; // const pointer to pointer to char
const char ** const p6; // const pointer to pointer to const char
char * const * const p7; // const pointer to const pointer to char
const char * const * const p8; // const pointer to const pointer to const char
typedef char * a; // a is a pointer to a char
typedef a b(); // b is a function that returns a pointer to a char
typedef b *c; // c is a pointer to a function that returns a pointer to a char
typedef c d(); // d is a function returning a pointer to a function that returns a pointer to a char
typedef d *e; // e is a pointer to a function returning a pointer to a function that returns a pointer to a char
e var[10]; // var is an array of 10 pointers to functions returning pointers to functions returning pointers
float ( * ( *b()) [] )(); // b is a function that returns a
// pointer to an array of pointers
// to functions returning floats.
void * ( *c) ( char, int (*)()); // c is a pointer to a function that takes
// two parameters:
// a char and a pointer to a
// function that takes no
// parameters and returns
// an int
// and returns a pointer to void.
void ** (*d) (int &,
char **(*)(char *, char **)); // d is a pointer to a function that takes
// two parameters:
// a reference to an int and a pointer
// to a function that takes two parameters:
// a pointer to a char and a pointer
// to a pointer to a char
// and returns a pointer to a pointer
// to a char
// and returns a pointer to a pointer to void
float ( * ( * e[10])
(int &) ) [5]; // e is an array of 10 pointers to
// functions that take a single
// reference to an int as an argument
// and return pointers to
// an array of 5 floats.
|
// CSomeClass.h
class CSomeClass{
public:
CSomeClass(void){};
~CSomeClass(void){};
char* GetFieldValue(int nField);
char* GetFieldName(int nField);
static char * GetFieldValueStatic(int nField);
static char * GetFieldNameStatic(int nField);
};
//CSomeClass.cpp
CSomeClass::CSomeClass(void){};
CSomeClass::~CSomeClass(void){};
char * CSomeClass::GetFieldValue(int nField){ return "Field Value stub";}
char * CSomeClass::GetFieldName(int nField){ return "Field Name stub";}
char * CSomeClass::GetFieldValueStatic(int nField){ return "Static Field Value stub";}
char * CSomeClass::GetFieldNameStatic(int nField){ return "Static Field Name stub";}
//
// Win32 console
// gFncTest and gFncTestStatic are global functions in question.
// Parentheses around (CSomeClass::*funPName) are required since * binds
// less tightly, has lower precedence, than the function call.
//
void gFncTest( char * (((CSomeClass::*funPName))(int)), CSomeClass* cp )
{
for ( int k =0; k < MAX_ITEMS_TO_PRINT; k++){ printf((cp->*funPName)(k)); printf("\n");}
return;
}
void gFncTestStatic ( char* (*kk)(int) ){
for ( int k =0; k < MAX_ITEMS_TO_PRINT; k++) { printf(kk(0)); printf("\n");}
return;
}
int main(int argc, _TCHAR* argv[]){
// without qualifier 'static' compiler reports:
// cannot convert parameter 1 from
// 'char *(__thiscall COracle::* )(int)' to 'char *(__cdecl *)(int)'
gFncTestStatic (&COracle::GetFieldValueStatic);
CSomeClass * cp = new CSomeClass();
// invoke this way
gFncTest (&CSomeClass::GetFieldName, cp);
gFncTest (&CSomeClass::GetFieldValue, cp);
// or invoke this way
char * (CSomeClass::* functPointName)(int) = &CSomeClass::GetFieldName;
gFncTest( (functPointName), cp );
delete cp;
return (0);
}
|
#include <cstddef>
#include <iostream>
using namespace std;
template<class T>
class Array { // A simple, expandable sequence
enum { INIT = 10 };
T* data;
size_t capacity;
size_t count;
public:
Array() {
count = 0;
data = new T[capacity = INIT];
}
~Array() { delete [] data; }
void push_back(const T& t) {
if(count == capacity) {
// Grow underlying array.
size_t newCap = 2 * capacity;
T* newData = new T[newCap];
// copy existing data to a bigger, newer buffer. Not very efficient CPU cycle wise.
for(size_t i = 0; i < count; ++i)
newData[i] = data[i];
delete [] data;
data = newData;
capacity = newCap;
}
data[count++] = t;
}
void pop_back() {
if(count > 0)
--count;
}
T* begin() { return data; }
T* end() { return data + count; }
};
template<class T, template<class> class Seq>
class Container {
Seq<T> seq;
public:
void append(const T& t) { seq.push_back(t); }
T* begin() { return seq.begin(); }
T* end() { return seq.end(); }
};
int main() {
Container<int, Array> container;
container.append(1);
container.append(2);
int* p = container.begin();
while(p != container.end())
cout << *p++ << endl;
} ///:~
|