注册 登录
编程论坛 C++教室

函数返回值

wskiawv 发布于 2008-12-14 18:24, 1339 次点击
大家先看看例子:
class Student
{
private:
char num[6];
char *Get()
{
    return num;
}
};
int main()
{  student s1;
s1.Get();
return 0;
}
其中s1.Get()的函数返回值是什么?函数char *Get()前面加个*号是什么用法?怎样才能获得私有数据成员num的数值.
9 回复
#2
newyj2008-12-14 18:57
返回 数组首地址
#3
chendianbo2008-12-14 19:12
返回的是num字符数组的首地址,得到num的值:num[0]、num[1]、num[2].分别表示为:*s1.Get()
、*(s1.Get()+1)、*(s1.Get()+2)
#4
无缘今生2008-12-14 23:31
首先说明:楼主的程序不能通过编译:因为Get函数是私有的。

第一个问题楼上两位都已经回答了,我说一下第二个问题:

Get函数已经返回了字符数组num的首地址,就相当于已经得到了类Student的私有数据成员的值了,因为参地址取值就可以了。(*num)
#5
wskiawv2008-12-15 00:38
class Student
{
private:
char num[6];
public:
char *Get()
{
    return num;
}
};
int main()
{  student s1;
s1.Get();
return 0;
}
Get()函数的公有的
#6
wskiawv2008-12-15 00:38
class Student
{
private:
char num[6];
public:
char *Get()
{
    return num;
}
};
int main()
{  student s1;
s1.Get();
return 0;
}
Get()函数的公有的
#7
hitcolder2008-12-15 12:15
回复 楼主 wskiawv 的帖子
不知道这样可不可以得到num的值啊?
class Student
{
private:
char num[6];
public:
char *Get()
{
    return num;
}
};
int main()
{  student s1;
cout<<s1.Get();  //这样不知道可不可以啊 ?
return 0;
}
#8
hitcolder2008-12-15 12:18
楼主还要区分大小写啊,Student和student第一个字母不一样啊,仔细啊
#9
wskiawv2008-12-15 16:56
hitcolder
我做过了可以直接调用得到num的值.
1