#include <stdio.h>
#include <windows.h>
class testclass
{
int i;
int j;
public:
virtual void kkk();
};
void testclass::kkk()
{
printf("testclass::kkk test/n");
}
struct teststruct : public testclass
{
int i;
int j;
void kkk();
};
void teststruct::kkk()
{
printf("teststruct::kkk test/n");
}
void (*testFunc)();
void initFun(void (*fun)())
{
testFunc = fun;
printf("call testFunc/n");
testFunc();
}
extern "C" void fun()
{
printf("call fun/n");
}
int main(int argc, char **argv)
{
testclass *pTestClass = new testclass();
pTestClass->kkk();
//pTestClass->i = 5; // for i is private
teststruct *pTestStruct = new teststruct();
pTestStruct->kkk();
pTestStruct->i = 6;
initFun(fun);
return 1;
}
输出结果:
testclass::kkk test
teststruct::kkk test
call testFunc
call fun
总体看来: c++ class 和struct差别主要在对成员变量的存取权限上.