C++ 如何在类对象内部分配数组
当然,我有一个学生数组。如何初始化分配给学生姓名[]的数组大小?我应该使用指针还是数组C++ 如何在类对象内部分配数组,c++,arrays,object,C++,Arrays,Object,当然,我有一个学生数组。如何初始化分配给学生姓名[]的数组大小?我应该使用指针还是数组 #include <iostream> #include "student.h" #include "course.h" int main(int argc, char** argv) { Student student[4]; Course computerClass(student); return 0; } #ifndef COURSE_H #define C
#include <iostream>
#include "student.h"
#include "course.h"
int main(int argc, char** argv) {
Student student[4];
Course computerClass(student);
return 0;
}
#ifndef COURSE_H
#define COURSE_H
#include "student.h"
class Course
{
private:
Student name[];
public:
Course();
Course(Student []);
};
#endif
#include "course.h"
#include <iostream>
using namespace std;
Course::Course()
{
}
Course::Course(Student []){
}
#包括
#包括“student.h”
#包括“课程h”
int main(int argc,字符**argv){
学生[4];
计算机课程(学生);
返回0;
}
#ifndef课程
#定义航向
#包括“student.h”
班级课程
{
私人:
学生姓名[];
公众:
课程();
课程(学生[]);
};
#恩迪夫
#包括“课程h”
#包括
使用名称空间std;
课程::课程()
{
}
课程:课程(学生[]){
}
只有在编译时知道数组大小时才能使用数组,如果不知道,则使用std::vector
:
#include <iostream>
#include "student.h"
#include "course.h"
int main(int argc, char** argv) {
Students students(4);
Course computerClass(students);
return 0;
}
#ifndef COURSE_H
#define COURSE_H
#include "student.h"
typedef std::vector<Student> Students;
class Course
{
private:
Students names;
public:
Course();
Course(const Students &students);
};
#endif
#include "course.h"
#include <iostream>
using namespace std;
Course::Course()
{
}
Course::Course(const Students &students) : names( students )
{
}
#包括
#包括“student.h”
#包括“课程h”
int main(int argc,字符**argv){
学生(4);
计算机课程(学生);
返回0;
}
#ifndef课程
#定义航向
#包括“student.h”
向量学生;
班级课程
{
私人:
学生姓名;
公众:
课程();
课程(const学生和学生);
};
#恩迪夫
#包括“课程h”
#包括
使用名称空间std;
课程::课程()
{
}
课程:课程(const学生和学生):姓名(学生)
{
}
谢谢,但我想知道为什么在构造函数的参数中使用const?