c++中构造函数问题

LA_Lai 2012-11-13 08:28:44
#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){}
Studnet(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) 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);
};
#endif0


而这句话Student(const std::string &s) : name(s), scores(){}中,name(s)这个是什么意思
...全文
104 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
breakfisher 2012-11-13
  • 打赏
  • 举报
回复
将s赋给name作为name的value, 即相当于 name = s
转角天边 2012-11-13
  • 打赏
  • 举报
回复
引用 楼主 laxmay 的回复:
C/C++ code?1234567891011121314151617181920212223242526272829#ifndef STUDENTC_H#define STUDENTC_H #include <iostream>#include <string>#include <valarray>class Student{private: typedef s……
给成员变量name赋值为s
libralibra 2012-11-13
  • 打赏
  • 举报
回复
楼主请自行搜索: 初始化列表(Initializer list) Lippman的《C++ Primer》中提到在以下三种情况下需要使用初始化成员列表: 一,需要初始化的数据成员是对象的情况; 二,需要初始化const修饰的类成员; 三,需要初始化引用成员数据; 有时必须用带有初始化列表的构造函数: (1)成员类型是没有默认构造函数的类。若没有提供显示初始化式,则编译器隐式使用成员类型的默认构造函数,若类没有默认构造函数,则编译器尝试使用默认构造函数将会失败。 (2)const成员或引用类型的成员。因为const对象或引用类型只能初始化,不能对他们赋值。 (3)在继承里面,只有初始化列表可以构造父类的private成员。
muyangzuo 2012-11-13
  • 打赏
  • 举报
回复
构造Student对象之前,将参数s传到成员变量name属于的string类型的构造函数中,用来作为初始值构造name。

64,649

社区成员

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

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