파생 클래스 함수에서 부모 클래스 함수를 호출하는 방법은 무엇입니까?
C ++를 사용하여 파생 클래스에서 부모 함수를 호출하는 방법은 무엇입니까? 예를 들어,라는 클래스 parent
와 child
부모에서 파생 된 클래스 가 있습니다. 각 클래스에는 print
함수가 있습니다. 자녀의 인쇄 기능 정의에서 부모 인쇄 기능을 호출하고 싶습니다. 어떻게하면 되나요?
나는 명백한 것을 말할 위험을 감수 할 것입니다. 함수를 호출하면 기본 클래스에 정의되어 있으면 파생 클래스에서 자동으로 사용할 수 있습니다 ( private
).
파생 클래스에 동일한 시그니처를 가진 함수가있는 경우 기본 클래스 이름 뒤에 콜론 두 개를 추가하여이를 명확하게 할 수 있습니다 base_class::foo(...)
. Java 및 C #과 달리 C ++는 모호성을 유발할 수있는 다중 상속 을 지원하므로 C ++에는 "기본 클래스"( 또는 )에 대한 키워드 가 없습니다 .super
base
class left {
public:
void foo();
};
class right {
public:
void foo();
};
class bottom : public left, public right {
public:
void foo()
{
//base::foo();// ambiguous
left::foo();
right::foo();
// and when foo() is not called for 'this':
bottom b;
b.left::foo(); // calls b.foo() from 'left'
b.right::foo(); // call b.foo() from 'right'
}
};
덧붙여서, 기본 클래스 중 하나를 다른 것보다 참조 할 방법이 없기 때문에 동일한 클래스에서 두 번 직접 파생 할 수 없습니다.
class bottom : public left, public left { // Illegal
};
명명 된 부모 클래스 Parent
와 명명 된 자식 클래스가 주어지면 Child
다음과 같이 할 수 있습니다.
class Parent {
public:
virtual void print(int x);
}
class Child : public Parent {
void print(int x) override;
}
void Parent::print(int x) {
// some default behavior
}
void Child::print(int x) {
// use Parent's print method; implicitly passes 'this' to Parent::print
Parent::print(x);
}
참고 Parent
클래스의 실제 이름이 아닌 키워드입니다.
기본 클래스가 호출 Base
되고 함수가 호출 되면 다음을 FooBar()
사용하여 직접 호출 할 수 있습니다.Base::FooBar()
void Base::FooBar()
{
printf("in Base\n");
}
void ChildOfBase::FooBar()
{
Base::FooBar();
}
MSVC에는 이에 대한 Microsoft 특정 키워드가 있습니다. __super
MSDN : 재정의하는 함수에 대해 기본 클래스 구현을 호출하고 있음을 명시 적으로 나타낼 수 있습니다.
// deriv_super.cpp
// compile with: /c
struct B1 {
void mf(int) {}
};
struct B2 {
void mf(short) {}
void mf(char) {}
};
struct D : B1, B2 {
void mf(short) {
__super::mf(1); // Calls B1::mf(int)
__super::mf('s'); // Calls B2::mf(char)
}
};
기본 클래스 멤버 함수의 액세스 한정자가 protected 또는 public이면 파생 클래스에서 기본 클래스의 멤버 함수를 호출 할 수 있습니다. 파생 멤버 함수에서 기본 클래스 비가 상 및 가상 멤버 함수를 호출 할 수 있습니다. 프로그램을 참조하십시오.
#include<iostream>
using namespace std;
class Parent
{
protected:
virtual void fun(int i)
{
cout<<"Parent::fun functionality write here"<<endl;
}
void fun1(int i)
{
cout<<"Parent::fun1 functionality write here"<<endl;
}
void fun2()
{
cout<<"Parent::fun3 functionality write here"<<endl;
}
};
class Child:public Parent
{
public:
virtual void fun(int i)
{
cout<<"Child::fun partial functionality write here"<<endl;
Parent::fun(++i);
Parent::fun2();
}
void fun1(int i)
{
cout<<"Child::fun1 partial functionality write here"<<endl;
Parent::fun1(++i);
}
};
int main()
{
Child d1;
d1.fun(1);
d1.fun1(2);
return 0;
}
산출:
$ g++ base_function_call_from_derived.cpp
$ ./a.out
Child::fun partial functionality write here
Parent::fun functionality write here
Parent::fun3 functionality write here
Child::fun1 partial functionality write here
Parent::fun1 functionality write here
부모 범위 확인 연산자를 사용하여 부모 메서드를 호출합니다.
Parent :: method ()
class Primate {
public:
void whatAmI(){
cout << "I am of Primate order";
}
};
class Human : public Primate{
public:
void whatAmI(){
cout << "I am of Human species";
}
void whatIsMyOrder(){
Primate::whatAmI(); // <-- SCOPE RESOLUTION OPERATOR
}
};
struct a{
int x;
struct son{
a* _parent;
void test(){
_parent->x=1; //success
}
}_son;
}_a;
int main(){
_a._son._parent=&_a;
_a._son.test();
}
참고 예.
'programing tip' 카테고리의 다른 글
라벨 요소 안에 입력 요소를 넣어야합니까? (0) | 2020.10.04 |
---|---|
자바 스크립트 클로저 vs. 익명 함수 (0) | 2020.10.04 |
ng-model과 ng-bind의 차이점은 무엇입니까 (0) | 2020.10.04 |
숫자 만 허용하는 텍스트 상자는 어떻게 만듭니 까? (0) | 2020.10.04 |
.key 및 .crt 파일에서 .pem 파일을 가져 오는 방법은 무엇입니까? (0) | 2020.10.04 |