关于valarray的问题?

qsq_007 2011-12-16 10:50:15
#ifndef STUDENTC_H_
#define STUDENTC_H_

#include <iostream>
#include <string>
#include <valarray>
class Student
{
private:
typedef std::valarray<double> ArrayDb;
std::string name;
ArrayDb scores;
std::ostream & arr_out(std::ostream & os) const;
public:
Student():name("Null Student"),scores() {}
Student(const std::string & s)
:name(s),scores(){}
explicit Student(int n):name("Nully"),scores(n){}
Student(const std::string & s,int n)
:name(s),scores(n){}
Student(const std::string & s,const ArrayDb & a)
:name(s),scores(a){}
Student(const char * str,const double * pd,int n)
:name(str),scores(pd,n){}
~Student(){}
double Average() const;
const std::string & Name() const;
double & operator[](int i);
double operator[](int i) const;
friend std::istream & operator>>(std::istream & is,
Student & stu);
friend std::istream & getline(std::istream & is,
Student & stu);
friend std::ostream & operator<<(std::ostream & os,
const Student & stu);
};
#endif
以上是Student类,其中的两个函数定义:
double & Student::operator[](int i)
{
return scores[i]; //use vallarray<double>::operator[]()
}
double Student::operator[](int i) const
{
return scores[i];
}
这两个函数有点不,明白第一个函数为什么多个引用操作符,第二个函数为什么多个const限定符?请大牛指点迷津!
...全文
49 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
游戏设计师 2011-12-16
  • 打赏
  • 举报
回复
应该是一个作为左值一个作为右值
引用是用来接受赋值的,const返回是用来赋值
qsq_007 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dyangrun 的回复:]

应该是一个作为左值一个作为右值
引用是用来接受赋值的,const返回是用来赋值
[/Quote]
了了!

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧