C++ 멤버 이니셜라이저에서의 생성자 호출과 관련된 질문

Era의 이미지

제 C++ 공부가 얕아 이런 질문을 하게 됐습니다.

#include<iostream>
#include<string>
 
using namespace std;
 
class Temp{
public:
	void Tempshow(){
		cout << "임시 부모 클래스" << endl;
	}
};
 
class Person{
private:
	string name;
public:
	Person(string name): name(name){}
	string getName(){
		return name;
	}
	void showName(){
		cout << "이름 : " << getName() << endl;
	}
};
 
class Student : Person, public Temp {
private:
	int studentID;
public:
	Student(int studentID, string name) : Person(name){
		this->studentID = studentID;																
	}
 
	// Student(int studentID, string name){
	// 	Person(name);
	// 	this->studentID = studentID;
	// }
	// Student(int studentID, string name){
	// 	this->studentID = studentID;
	// }
 
	void show(){
		cout << "학생번호 : " << studentID << endl;
		cout << "학생이름 : " << getName() << endl;
 
	}
	void showName(){
		cout << "상속이 성공된 학생이름 : " << getName() << endl;
	}
};
 
int main(){
	Student student = Student(1, "James");
	student.showName();
	student.Tempshow();
	return 0;
}

일단 위 코드에서 Student 생성자 아래의 주석처리한 두 문장이 왜 작동하지 못하는 지 궁금합니다.

제가 생각하기에는 클래스의 생성자에서 파라미터로 받는 studentID와 name이 그저 파라미터로서 생성자 내부에서만 작동하는 게 아니라, 멤버변수 그대로의 의미라서, 아래와 같이 Student 생성자에 파라미터를 적어서 호출해도, Person 생성자에 "James"가 넘어가서 먼저 호출된다...라고 추정은 하고 있습니다. 맞는지 확인해주셨으면합니다.

Student(int studentID, string name) : Person(name){
	this->studentID = studentID;																
}
.
.
.
int main(){
	Student student = Student(1, "James");
	student.showName();
	student.Tempshow();
	return 0;
}

감사합니다.

익명 사용자의 이미지

Quote:
일단 위 코드에서 Student 생성자 아래의 주석처리한 두 문장이 왜 작동하지 못하는 지 궁금합니다.

제가 생각하기에는 클래스의 생성자에서 파라미터로 받는 studentID와 name이 그저 파라미터로서 생성자 내부에서만 작동하는 게 아니라, 멤버변수 그대로의 의미라서, 아래와 같이 Student 생성자에 파라미터를 적어서 호출해도, Person 생성자에 "James"가 넘어가서 먼저 호출된다...라고 추정은 하고 있습니다. 맞는지 확인해주셨으면합니다.

무슨 뜻인지 모르겠습니다.

======

주어진 코드에서 Student class의 정석적인 생성자 형태는 뭐 이렇습니다.

Student(int studentID, string name): Person(name), studentID(studentID){
	/* do nothing */
}

제시하신 코드와 같이 생성자 body에서 this->studentID = studentID; 이렇게 하면 안 된다고까지 말씀드리지는 않겠습니다만... 왜 굳이...?

======

주석 처리된 Student의 생성자는 컴파일이 안 되는 게 정상입니다.

Student 객체를 초기화하려면 먼저 base class인 Person 부분이 초기화가 되어야 하는데, Person은 매개변수를 받지 않는 기본 생성자 (default construction)가 없잖아요.

즉 Person 부분을 초기화하기 위해서 반드시 매개변수를 넘겨 주어야 하고, 그렇게 하려면 initializer lists를 쓰는 수밖에 없습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.