How can I initialize array in Class's constructor

Hi ,

As far as I know if we want to pass array to function there are two ways. One is we need to pass array name and size as an argument and the other one is only pass array member. But in the following code of class GradeBook , I found that it pass the array differently. I am wondering about this. The codes are as follow. The codes are from headerfile.

GradeBook.h

class GradeBook
{
private :
string courseName;
int grades[students];

public:
static const int students =10 ;

GradeBook(string , const int [] )// This is the line of code that I am wondering

void setCourseame(string);
void displayMessage() ;
string getCourseName() ;
void porcessGrade () ;
};
In this case, the assumption is that you are always passing a fixed size array around (10). It's dangerous.

Now, how do you initialize the array? You need to do a copy it into the grades array.

1
2
3
4
5
GradeBook(string name, const int[] g)
: courseName(name), grades()
{
    std::copy(g, g+students, grades);
}
Topic archived. No new replies allowed.