// Inheritance.cpp : main project file.
#include "stdafx.h"
using namespace System;
ref class Base {
private:
int value;
int value2;
Base() { this->value2 = 4; }
protected:
Base(int n) {
Base(); // <- here is my problem
value = n;
}
int get(){ return value; }
int get2(){ return value2; }
};
ref class Derived : Base {
public:
Derived(int n) : Base(n) { }
void println(){
Console::WriteLine(Convert::ToInt32(get()));
Console::WriteLine(Convert::ToInt32(get2()));
}
};
int main(array<System::String ^> ^args) {
Derived ^obj = gcnew Derived(5);
obj->println();
Console::ReadLine();
}
콘솔 출력은 다음과 같습니다C++ 생성자, 상속, 액세스 수정 및 재료
0
5
나는 기본() 생성자를 호출 할 것을 알고, 나는 새로운 객체 같은 것을 만드는 것을 알고있다 Base (int n)가 호출 된 후에 사라지는 ...
그러나 나는 개인 기본 생성자와 보호 된 개체를 어떻게 조합 할 수 있는지 잘 모릅니다.
(난 비주얼 스튜디오 2010를 통해 .NET 프레임 워크를 사용하고,하지만 난이 더 일반적인 C++ 문제처럼 생각)
C++에서는 생성자를 호출 할 수 없습니다 생성자에서 : http://stackoverflow.com/questions/308276/c-call-constructor-from-constructor – birryree
링크를 사용해 주셔서 감사합니다. – Baarn
@birryee : 확실히 할 수 있습니다. 다른 개체에 대한 현재 개체의 생성 –