首页 新闻 会员 周边

在类中用类定义一个函数是用作什么,怎么理解

0
悬赏园豆:10 [已解决问题] 解决于 2011-11-24 22:06

#include <iostream.h>
#include <string.h>
class student
{
public:
 student(char *name);
 ~student();
 static student *findname(char *name);
protected:
 char *stname;
 static student *first;
 student *next;

};

static student *findname(char *name);这句为什么要用student来定义?他是属于什么用法??谢谢各位!

毅青的主页 毅青 | 初学一级 | 园豆:191
提问于:2011-11-23 15:48
< >
分享
最佳答案
0

静态成员函数,可以这样调用:

student::findname("xxx");

也可以这样

student a;

a.findname("xxx");

收获园豆:5
Greatest | 小虾三级 |园豆:678 | 2011-11-23 17:55

但是那个 student *next;他不是静态成员函数,为什么在class student{}里面中有又用到了         student *next,我试过那个定义student next是不可以编译成功的,为什么?谢谢

毅青 | 园豆:191 (初学一级) | 2011-11-23 21:22

@毅青: 因为定义student next的时候,在class student内部,这时候student没有complete。也就是说编译器不知道sizeof(student)的大小。而如果用student *next就没问题,因为指针的大小是固定的。

Greatest | 园豆:678 (小虾三级) | 2011-11-24 10:04
其他回答(2)
-1

为了支持链式表达式,例如

student a;

a.findname("you!")->giveMeAGun();

收获园豆:2
天使的白骨 | 园豆:207 (菜鸟二级) | 2011-11-24 14:23
0

static student *findname(char *name); 这句很直白的啦, findname是根据name查找一个student, 当然返回可以student *啦,另外,查找这个工作不需要有一个student, 当然就声明为静态的啦;其实findname这个签名最好改为findByName更加确切些,看程序时,要注意程序的语义! 随便哪本C++语法书上都会给你上面语法的解释。

收获园豆:3
ChatinCode | 园豆:2272 (老鸟四级) | 2011-11-24 14:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册