J's Study log

[C++] Operation principle of Member Function & Virtual Function 본문

Computer Language/C++

[C++] Operation principle of Member Function & Virtual Function

정우섭 2020. 4. 25. 23:25

When you make an object by using a class.

 

You might think that the memory of member functions are placed in the memory of object.

 

But actually it's not.

 

Conceptually, and on the basis of OOP logic, it's natural to think of member function does exist in the object.

 

And it's a great attitude for facing C++.

 

So just put the truth at the back of your mind.

 

Let's cut to the chase.

 

 

Operation principle of Member Function

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 
using namespace std;
 
class Data{
private:
    int data;
public:
    Data(int num): data(num){};
    void ShowData(){cout << "Data : " << data << endl}
    void Add(int num){data+=num}
};
 
int main(){
    Data obj(15);
    obj.Add(17);
    obj.ShowData();
    return 0;
}
 

 

To explain the operation principle of member function.

 

Let's imitate this simple class in C style to see how member function and member variable formed.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
 
using namespace std;
 
typedef struct Data{
    int data;
    void(*ShowData)(Data*); //function pointer variable memorizes the structure member's address
    void (*Add)(Data*int); //function pointer variable memorizes the structure member's address
} Data;
 
void ShowData(Data* THIS){cout <<"Data : " << THIS->data << endl;}
void Add(Data* THIS, int num){THIS->data+=num;}
 
int main(){
    Data obj1 = {15, ShowData, Add};
    Data obj2 = {7, ShowData, Add};
 
    obj1.Add(&obj1, 17);
    obj2.Add(&obj2, 9);
    obj1.ShowData(&obj1);
    obj2.ShowData(&obj2);
    return 0;
}
 

 

Result

 
 
Data: 32
Data: 16
 

 

It's just an imitation for explaining so it's not Data class itself.

And you need knowledge of function pointer.

 

 

<윤성우의 열혈 C++ 프로그래밍> 中

The member function allocates at a part of memory and every object declared by the class shares it.

 

While compiling, each object call the function and the algorithm proceeded by the member variables.

 

 

Operation principle of Virtual Function and the V-table

 

By understanding operation principle of virtual function, you'll get one of the reasons of why C++ is slower then C.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
 
using namespace std;
 
class AAA
{
private:
    int num1;
public:
    virtual void Func1() {cout<<"Func1"<<endl;}
    virtual void Func2() {cout<<"Func2"<<endl;}
};
 
class BBB : public AAA
{
private:
    int num2;
public:
    virtual void Func1() {cout<<"BBB::Func1"<<endl;}
    void Func3() {cout<<"Func3"<<endl;}
};
 
int main(void)
{
    AAA* aptr = new AAA();
    aptr->Func1();
    
    BBB* bptr = new BBB();
    bptr->Func1();
    return 0;
}
 
 

 

Result

 
 
Func1
BBB::Func1
 

 

Classes which contains virtual function, even there is only one of them like AAA class, are algorithmically designed to make 'V-table'.

 

V-table is a table of information where the address of member function's memory is located.

 

 

V-table of AAA class

 key  value
void AAA::Func1(  ) 0x1024
void AAA::Func2(  ) 0x2048

 

V-table of BBB class

 key value
void BBB::Func1(  )​ 0x3072
void AAA::Func2(  )​ 0x2048
void BBB::Func3(  )​ 0x4096

 

V-table is consist of key and value.

 

Which function to call is decided by key and value show the address of the function.

 

In BBB V-table there is no information about AAA::Func1(  ).

 

Latest overrided member function's key&value is in V-table, so in class BBB V-table there's only key of BBB::Func1(  ).

 

 

 

 

 

 

 

 

[참고자료]

- 윤성우의 열혈 C++ 프로그래밍

'Computer Language > C++' 카테고리의 다른 글

[C++] Default Value of the Parameter  (0) 2020.05.02
[C++] Multiple Inheritance  (0) 2020.04.30
[C++] Virtual Function  (0) 2020.04.01
[C++] Reference Relation of Object Pointer  (0) 2020.03.27
[C++] Function Overloading & Overriding  (0) 2020.03.22
Comments