구조체 포인터값을 구조체 래퍼런스 함수 값으로 바꾸는 방법 문의 드립니다.
조회수 398회
void PrintOneData(Student* stu)
{
cout << "이름 : " << stu->stdName << " 학번 : " << stu->hakbun << endl;
cout.width(10); cout << "========================================================" << endl;
cout.width(20); cout << "과목명";
cout.width(10); cout << "과목학점";
cout.width(10); cout << "과목등급";
cout.width(10); cout << "과목평점" << endl;
cout.width(10); cout << "========================================================" << endl;
for (int j = 0; j < 3; j++)
{
cout.width(20); cout << stu->sub[j].subName;
cout.width(7); cout << stu->sub[j].hakjum;
cout.width(10); cout << stu->sub[j].grade;
cout.width(11); cout << stu->sub[j].gpa << endl;
}
cout << "========================================================" << endl;
stu->ave_gpa = (stu->sub[0].gpa + stu->sub[1].gpa + stu->sub[2].gpa) / 3;
cout.width(46); cout << "평균평점 : " << stu->ave_gpa << endl;
cout << endl << endl;
}
위의 코드를 아래의 수정할라고 하는데 어떤 소스를 더 추가 시켜줘야하나요?
void PrintOneData(const Student& stu)
{
cout << "이름 : " << stu->stdName << " 학번 : " << stu->hakbun << endl;
cout.width(10); cout << "========================================================" << endl;
cout.width(20); cout << "과목명";
cout.width(10); cout << "과목학점";
cout.width(10); cout << "과목등급";
cout.width(10); cout << "과목평점" << endl;
cout.width(10); cout << "========================================================" << endl;
for (int j = 0; j < 3; j++)
{
cout.width(20); cout << stu->sub[j].subName;
cout.width(7); cout << stu->sub[j].hakjum;
cout.width(10); cout << stu->sub[j].grade;
cout.width(11); cout << stu->sub[j].gpa << endl;
}
cout << "========================================================" << endl;
stu->ave_gpa = (stu->sub[0].gpa + stu->sub[1].gpa + stu->sub[2].gpa) / 3;
cout.width(46); cout << "평균평점 : " << stu->ave_gpa << endl;
cout << endl << endl;
}
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
C++에서 멤버 접근 연산자는
.
와->
가 있습니다. 이 중->
는 포인터에서 사용되며, 그외에는.
을 사용합니다.예를 들어보면 다음과 같습니다.
std::string* pValue = new std::string(); pValue->size(); std::string value; value.size(); std::string& refvalue = *pValue; refvalue.size();
pValue
는 포인터입니다. 포인터가 가리키는 객체의 멤버는 앞서 말씀드렸듯이->
을 이용합니다.value
는 일반 변수입니다. 포인터가 아니기에.
을 사용합니다.refvalue
는 레퍼런스입니다. 역시 포인터가 아니기에.
을 사용합니다.
질문의 코드를 보면
stu
가 포인터에서 레퍼런스로 변경 되었습니다. 그에 따라stu->sub
을stu.sub
처럼 변경이 필요합니다.
댓글 입력