m_i 값이 안바뀌는 이유가 뭔가요?
358
작성한 질문수 2
//
// main.cpp
// inheritance_ex01
//
// Created by Kevin on 04/10/2019.
// Copyright © 2019 Kevin. All rights reserved.
//
#include <iostream>
using namespace std;
class Mother { // basic class
private:
int m_i;
public:
//Mother() {}
Mother(const int& i_in) : m_i(i_in) {
std::cout << "Mother constructor" << std::endl;
std::cout << "m_i " << m_i << std::endl;
}
void setValue(const int& i_in) {
std::cout << "i_in " << i_in << std::endl;
m_i = i_in;
std::cout << "m_i " << m_i << std::endl;
}
int getValue() {
return m_i;
}
};
class Child : public Mother {
private:
double m_d;
public:
Child(const int& i_in, const double& d_in) : Mother(i_in), m_d(d_in) {
std::cout << "Child constructor" << std::endl;
Mother::setValue(i_in);
}
void setValue(const int& i_in, const double& d_in) {
}
void setValue(const double& d_in) {
m_d = d_in;
}
double getValue() {
return m_d;
}
}; // derived class
int main(int argc, const char * argv[]) {
Mother mother(512);
std::cout << mother.getValue() << std::endl;
Child child(1024, 2048);
std::cout << "m_i value : " << mother.getValue() << std::endl;
// 왜 512인가요?
std::cout << "m_d value : " << child.getValue() << std::endl;
return 0;
}
답변 4
1
원인은 찾았는데, 개념적인 이해를 이렇게 해도 될까요?
mother에서 상속받은 m_i는 mother의 m_i를 상속 받으면, child에 별개의 상속받은 m_i가 있다고 보면 되는 것인가요?
상속을 받으면, 동일한 m_i를 공유 한다고 생각을 했었습니다.
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
464
1
메모리 주소 10진수로 출력
1
653
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
498
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
534
1
메모리 주소에 관한 질분
0
679
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
549
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
443
1
Digit 뒤에 reference를 사용하는 이유
0
509
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
322
1
dat파일이...
0
538
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
643
1
복사 생성자 관련 질문이 있습니다.
0
454
1
수업 중 궁금한점이 있습니다.
1
389
1
라이브러리자체가 이해가 되지 않습니다.
0
561
1
마지막 예제 질문
0
302
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
374
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
410
1
friend함수 관련 질문이 있습니다.
0
311
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
447
1
3분 17초 질문
0
350
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
447
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
443
2
const Something &st에서 const를 빼면 안되나요?
0
300
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
492
1





