从下例子中可以得出,象下面这个例子声明一个结构后,stdent,class1student,pclstu在主函数里作为声明类型用,不能够看做已经是一个变量来用
#include <stdio.h>
#include <malloc.h>
typedef struct student
{
char *name;
int age;
}class1student,*pclstu;
void main()
{
pclstu mypclstu;//还可以是class1student(student) *mypclstu
mypclstu=(struct student *)malloc(sizeof(student));
mypclstu->age=20;
mypclstu->name="frankie";
printf("the student name is %s,and age is %d/n",mypclstu->name, mypclstu->age);
}
从下面的例子可以看出
#include <stdio.h>
#include <malloc.h>
typedef struct student
{
char *name;
int age;
}class1student,*pclstu;
//int func(class1student const *myclass)//这样是出错的,因为const修饰*myclass,而在函数中是要修改*myclass的,改成class1student const *myclass就正确了
{
myclass->age=myclass->age*2;
return myclass->age;
}
//int func(class1student &myclass)//如果参数是引用,则主调函数用传个指针给它也是错的啊!!,如果改成const class1student &myclass,出错!因为下面修改了&myclass
可是 class1student & const myclass可以改变被引用的值,试问和指针的机制有何不同。
{
myclass.age=myclass.age*2;
return myclass.age;
}
void main()
{
pclstu mypclstu;//还可以是class1student(student) *mypclstu
mypclstu=(struct student *)malloc(sizeof(student));
mypclstu->age=20;
mypclstu->name="frankie";
func(mypclstu);
printf("the student name is %s,and age is %d/n",mypclstu->name, mypclstu->age);
}
////////////
从递推方法中的到非递推方法
/////////////////
请再次了解公有继承,私有继承的概念
//////////////
养成构造函数和初始化函数分开的习惯,养成定义基类的typedef *** Base的习惯