Compile์—์„œ virtual๊นŒ์ง€

์ปดํŒŒ์ผ๋Ÿฌ/๋งํ‚น/ELF๋“ฑ๋“ฑ ๋‚ด์šฉ์€ ํ๋ฆ„์— ํ•„์š”ํ•œ ๋‚ด์šฉ๋งŒ ๊ฐ„๋žตํ•˜๊ฒŒ ์ ์„ ์˜ˆ์ •.

์ฐธ๊ณ  ์„œ์  ๋ฐ ๋‚ด์šฉ : ์ปดํŒŒ์ผ๋Ÿฌ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋“ค๋ ค์ฃผ๋Š” C์ด์•ผ๊ธฐ, []

๊ทธ๋ฆฌ๊ณ , ํ•ด๋‹น ํŒŒ์ผ๋“ค์„ ์ปดํŒŒ์ผ๋Ÿฌ๋กœ ํ•˜์—ฌ ์‹คํ–‰ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋‚ผ๊ฒƒ์ด๋‹ค.

์ปดํŒŒ์ผ๋Ÿฌ๋Š” ~~์˜ ์ง‘ํ•ฉ๋‹ˆ๋‹ค.

1.1 ์ „์ฒ˜๋ฆฌ๊ธฐ

  1. .cํŒŒ์ผ์˜ #include๊ฐ€ ๊ฐ ํ—ค๋”์˜ ๋‚ด์šฉ๋“ค๋กœ ์น˜ํ™˜๋œ๋‹ค.
  2. .cํŒŒ์ผ์˜ ๋งคํฌ๋กœ๋ฅผ #define์˜ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ์น˜ํ™˜ํ•œ๋‹ค.

์ด์™ธ์— #undef, #ifdef, #error, #pragma, #lineโ€ฆ๋“ฑ๋“ฑ์„ ์ฒ˜๋ฆฌํ•œ .iํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

1.2 ์ปดํŒŒ์ผ

์ปดํŒŒ์ผ๋Ÿฌ๋Š” .iํŒŒ์ผ์„ ํ•ด์„ํ•˜์—ฌ ๊ณ ์ˆ˜์ค€์˜ ์–ธ์–ด๋ฅผ ์ €์ˆ˜์ค€์˜ ์–ธ์–ด(์–ด์…ˆ๋ธ”๋ฆฌ)๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์ƒ๊ธด๊ฒƒ์ด .sํŒŒ์ผ์ด๋ฉฐ, ์‚ฌ์šฉ์ž ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์ €์ˆ˜์ค€ ์–ธ์–ด ๋™์ž‘ ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋ฏ€๋กœ ์„œ๋กœ๋‹ค๋ฅธ ์šด์˜์ฒด๊ฒŒ๊ฐ„์— ํ˜ธํ™˜๋˜์ง€ ์•Š๋Š”๋‹ค.

๊ณ ์ˆ˜์ค€์˜ ์–ธ์–ด๋ฅผ ์ €์ˆ˜์ค€์˜ ์–ธ์–ด๋กœ ํ•ด์„ํ•˜๋Š” ๋„์ค‘ ๋ฐœ์ƒํ•˜๋Š”๊ฒŒ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜์ด๋ฉฐ, ์ฝ”๋“œ์˜ ๋ฌธ๋ฒ•์ƒ์˜ ๋ฌธ์ œ์ด๋‹ค.

1.3 ์–ด์…ˆ๋ธ”

์–ด์…ˆ๋ธ”๋Ÿฌ๊ฐ€ ์–ด์…ˆ๋ธ”๋ฆฌ์–ด์ธ .sํŒŒ์ผ์„ ํ•ด์„ํ•˜์—ฌ ๊ธฐ๊ณ„์–ด(๋ฐ”์ด๋„ˆ๋ฆฌํŒŒ์ผ))๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์ด๋•Œ ์ƒ๊ธด๊ฒƒ์ด ์˜ค๋ธŒ์ ํŠธํŒŒ์ผ(.o, .obj)์ด๋‹ค.

์˜ค๋ธŒ์ ํŠธํŒŒ์ผ ๋ฐ ์‹คํ–‰ํŒŒ์ผ(์ถ”๊ฐ€๋กœ DDL)๋“ฑ์„ ์œ ๋‹‰์Šค/๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” ELFํŒŒ์ผ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์œˆ๋„์šฐ์—์„œ๋Š” PEํŒŒ์ผ์ด๋ผ๊ณ  ํ•œ๋‹ค. ํŠนํžˆ ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ์€ ์œˆ๋„์šฐ์—์„œ๋Š” PEํ˜•์‹์˜ .obj, ๋ฆฌ๋ˆ…์Šค/์œ ๋‹‰์Šค์—์„œ๋Š” ELFํ˜•์‹์˜ .o์ด ์ƒ์„ฑ๋œ๋‹ค.

๋Œ€๋žต์ ์ธ ํ˜•ํƒœ๋Š” ์•„๋ž˜ ๋งํ‚น ์ด๋ฏธ์ง€์— ํ‘œ์‹œ. ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ ๊นŒ๋ดค๋Š”๋ฐโ€ฆ .obj๋Š” ์ •๋ณด๋„ ์ ๊ณ , ๋ญ”๊ฐ€ ๊ตฌ์กฐ๋„ ๋‹ฌ๋ผ์„œ ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๋Š”๊ฒŒ ์ข‹์„๋“ฏ ํ•˜๋‹ค. ๋ฆฌ๋ฒ„์‹ฑ ๊ณต๋ถ€ํ•˜๋Š”๊ฑฐ ์•„๋‹ˆ๊ณ ์„  ์ž˜ ๋ชจ๋ฅด๊ฒ ์Œโ€ฆ

์šฐ๋ฆฌ์—๊ฒŒ ํ•„์š”ํ•œ๊ฑด ์˜ค๋ธŒ์ ํŠธ ํŒŒ์ผ์ด PEํ˜•์‹์ด๋ฉฐ, ๋ณ€์ˆ˜ ๋ฐ ํ•จ์ˆ˜๊ฐ€ ๊ฐ ๊ธฐ๋Šฅ๋ณ„ ์„น์…˜์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

https://redthing.tistory.com/entry/PE-%ED%8C%8C%EC%9D%BC%EC%9D%98-%EA%B5%AC%EC%A1%B0

1.4 ๋งํ‚น ๋ฐ ์‹คํ–‰ํŒŒ์ผ

linking 1 linking 2

๊ฐ ๋ณ€์ˆ˜ ๋ฐ ํ•จ์ˆ˜๊ฐ€ ์„น์…˜(.bss, .data, ๋“ฑ์— ๋”ฐ๋ผ ๊ตฌ๋ถ„)๋˜์–ด ์žˆ๊ณ , ๋งํ‚น๋•Œ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์™์™ ์ฑ™๊ฒจ์„œ ์‹คํ–‰ํŒŒ์ผ์— ํ•ฉ์นœ๋‹ค.

1.5 PEํŒŒ์ผ

.exe PEview PEํŒŒ์ผ์„ ๋ทฐ์–ด๋กœ ์—ด์–ด์„œ ํ™•์ธํ•œ ์ด๋ฏธ์ง€. ์ถœ๋ ฅ์„ ์œ„ํ•ด ์ฝ”๋“œ์— ๊ธฐ์ž…ํ•œ ๋ฌธ์ž์—ด์ด .rdata์˜์—ญ์— ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.

PEํŒŒ์ผ์€ ํ—ค๋”์™€ ๋ฐ”๋””๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๊ณ , ๋ฐ”๋””๋Š” ์„น์…˜์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

์„น์…˜๋ณ„ ์ •๋ณด ์ฐธ๊ณ 

2. ๋ฉ”๋ชจ๋ฆฌ ๋กœ๋”ฉ

PE to memory ์™ผ์ชฝ์ด PEํฌ๋ฉง. PEํŒŒ์ผ์˜ ์ž์„ธํ•œ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋Š” ํ•ด๋‹น ๋ธ”๋กœ๊ทธ์ฐธ์กฐ.

PEํŒŒ์ผ์ด ๋กœ๋“œ๋ ๋•Œ, VMM์€ ํŽ˜์ด์ง€ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , PEํŒŒ์ผ ์ž์ฒด๋ฅผ ํŽ˜์ด์ง€ํŒŒ์ผ์ฒ˜๋Ÿผ ๊ฐ€์ƒ ์ฃผ์†Œ ๊ณต๊ฐ„์— ๊ทธ๋Œ€๋กœ ๋งคํ•‘ ์ฐธ๊ณ 

ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ๋กœ ์˜ฌ๋ฆฌ๋Š”๊ฒƒ์„ ๋กœ๋“œ๋ผ๊ณ  ํ•˜๋ฉฐ, ์ด๋Š” ๋กœ๋”์— ์˜ํ•ด ์ˆ˜ํ–‰๋œ๋‹ค. ๋กœ๋”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

  1. ํŒŒ์ผ์˜ ์ •๋ณด๋ฅผ ์ฝ๊ณ  ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰
  2. ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ณ„์‚ฐ ๋ฐ ํ”„๋กœ์„ธ์Šค ์„ค์ •
    • ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ์œ„ํ•œ ๊ธฐ๋ณธ ๋ฉ”๋ชจ๋ฆฌ(ํ”„๋กœ์„ธ์Šค๋‹น ํ• ๋‹น๋œ)ํ• ๋‹น
    • ๋ณด์กฐ ์ €์žฅ์žฅ์น˜์—์„œ ๋ฉ”๋ชจ๋ฆฌ๋กœ ์ฃผ์†Œ๊ณต๊ฐ„์„ ๋ณต์‚ฌํ•œ๋‹ค.
    • ์‹คํ–‰ํŒŒ์ผ์˜ .text ๋ฐ .data์„น์…˜์„ ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋ณต์‚ฌํ•œ๋‹ค.

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด PE์˜ ์„น์…˜์„ ๊ฑฐ์˜ ๊ทธ๋Œ€๋กœ ๋ฉ”๋ชจ๋ฆฌ๋กœ ์˜ฌ๋ฆฌ๋Š”๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค PE๋Š” ๊ธฐ๊ณ„์–ด๋กœ ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ, ์ด ์ž์ฒด๊ฐ€ ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•˜๋Š” ๋™์ž‘ ๋ช…๋ น์ธ ๊ฒƒ์ด๋‹ค.

๋Œ€๋ถ€๋ถ„ ๊ทธ๋Œ€๋กœ ์˜ฌ๋ผ๊ฐ€์ง€๋งŒ, NULL์˜ ๊ฒฝ์šฐ NULL padding์„ ํ•˜์—ฌ ํฌ๊ธฐ๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค(ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๋งŒ๋“ฆ).

3. ํ”„๋กœ๊ทธ๋žจ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ.


์ด๋ฏธ์ง€ ์ฐธ์กฐ

์œ„์ฒ˜๋Ÿผ ๋ฉ”๋ชจ๋ฆฌ์ƒ์— ์˜ฌ๋ผ๊ฐ„ PEํŒŒ์ผ(ํ”„๋กœ๊ทธ๋žจ)์€ ํ”„๋กœ์„ธ์Šค์— ํ• ๋‹นํ•œ ๊ณต๊ฐ„์ค‘ .text, .data(.bssํฌํ•จ) ์˜์—ญ์ด ๋œ๋‹ค.

์Šคํƒ์€ ์ž„์‹œ์ ์ธ ๊ณต๊ฐ„์œผ๋กœ,

  1. ํ”„๋กœ๊ทธ๋žจ ์ธ์ˆ˜(ac, av, envโ€ฆ)๋ฅผ ์Šคํƒ์— ๋ณต์‚ฌํ•œ๋‹ค.
  2. esp๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ์œ„ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์—์„œ ์Šคํƒ์˜ ๊ฐ€์žฅ ์œ„(์‹œ์ž‘๋ถ€)๋ฅผ ๊ฐ€๋ฅดํ‚ค๋„๋ก ํ•œ๋‹ค.
  3. ์‹œ์ž‘๋ฃจํ‹ด(mian())์œผ๋กœ ์ ํ”„ํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๋™์ž‘ํ•˜๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค.

heap์ฃผ์†Œ๋Š” ์ฆ๊ฐ€ํ•˜๊ณ , stack์ฃผ์†Œ๋Š” ๊ฐ์†Œํ•˜๊ฒŒ ์„ค์ •ํ•˜์—ฌ, stack์ด๋‚˜ heap์ด overflowํ•ด๋„ ์ค‘์š” ๋ฐ์ดํ„ฐ๋Š” ์นจ๋ฒ”ํ•˜์ง€ ์•Š๊ฒŒ ์„ค๊ณ„๊ฐ€ ๋˜์–ด์žˆ๋‹ค.

์ด๋ฏธ์ง€ ๋ฐ ๊ฐœ๋… ์ฐจ์šฉ

2 virtual๊ณผ vtable

์šฐ์„  ๊ฐœ๋ฐœ์ž๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์€ ํŒŒ์ผ๋“ค์„ ์ž‘์„ฑํ•˜๊ณ , ์ปดํŒŒ์ผ์„ ํ•œ๋‹ค.

[Base.hpp]
class Base
{
public:
	int a;
	Base() :a(42) {}
	virtual void listen();
	void speak();
};
[Base.cpp]
#include <iostream>
#include "Base.h"

void Base::listen()
{
	std::cout << "Base listen" << std::endl;
}

void Base::speak()
{
	std::cout << "Base speak" << std::endl;
}
[Derived.hpp]
class Derived : public Base
{
public:
	int b;
	Derived() :b(43) { a = 41; }
	virtual void listen() override;
	void speak();
};
[Derived.cpp]
#include <iostream>
#include "Derived.h"

void Derived::listen()
{
	std::cout << "Derived listen" << std::endl;
}

void Derived::speak()
{
	std::cout << "Derived speak" << std::endl;
}
[main.cpp]
#include <iostream>
#include "Base.h"
#include "Derived.h"


int main()
{
	Base base;
	Derived derived;
	Base* basePtr = new Base();
	Derived* derivedPtr = new Derived();
	Base* derivedPoly = new Derived();
	//std::cout << << std::endl;
	
	std::cout << "base.a :" << base.a << std::endl;
	base.listen();
	base.speak();
	std::cout << "-----------------------------------" << std::endl;
	std::cout << "derived.a :" << derived.a << std::endl;
	std::cout << "derived.b :" << derived.b << std::endl;
	derived.listen();
	derived.speak();
	std::cout << "-----------------------------------" << std::endl;
	std::cout << "basePtr->a :" << basePtr->a << std::endl;
	basePtr->listen();
	basePtr->speak();
	std::cout << "-----------------------------------" << std::endl;
	std::cout << "derivedPtr->a :" << derivedPtr->a << std::endl;
	std::cout << "derivedPtr->b :" << derivedPtr->b << std::endl;
	derivedPtr->listen();
	derivedPtr->speak();
	std::cout << "-----------------------------------" << std::endl;
	std::cout << "basePtr->a :"  << derivedPoly->a << std::endl;
	// dynamic_cast -> ๋™์ ๋•Œ ๊ฒฐ์ •๋จ.
	std::cout << "dynamic_cast<Derived*>(basePtr)->a :" << dynamic_cast<Derived*>(derivedPoly)->a << std::endl;
	std::cout << "dynamic_cast<Derived*>(basePtr)->b :" << dynamic_cast<Derived*>(derivedPoly)->b << std::endl;
	derivedPoly->listen();
	derivedPoly->speak();
	std::cout << "dynamic_cast<Derived*>(basePtr)->listen() :";
	dynamic_cast<Derived*>(derivedPoly)->listen();
	std::cout << "dynamic_cast<Derived*>(basePtr)->speak() :";
	dynamic_cast<Derived*>(derivedPoly)->speak();
}

code์˜์—ญ์— vtable ์žˆ๋‹ค. ๊ฐ ํด๋ž˜์Šค๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ํ• ๋‹น๋˜๋ฉฐ ๊ทธ ์œ„์น˜๋Š”โ€ฆ

๊ฐ virtual์ด ์žˆ๋Š” ์ธ์Šคํ„ด์Šค์˜ ์‹œ์ž‘ ์ฃผ์†Œ์—๋Š” 8byte(62bit์ฒด๊ณ„)ํฌ๊ธฐ์˜ vtable์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์–ด๋–ป๊ฒŒ ํ•จ์ˆ˜๊ฐ€ vtable์— ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์ ‘๊ทผ์„ ํ•˜์ง€?

2.1 ๋‹คํ˜•์„ฑ, ์ปดํŒŒ์ผํƒ€์ž„, ๋Ÿฐํƒ€์ž„ ๊ทธ๋ฆฌ๊ณ  vtable์— ๋Œ€ํ•œ ๊ณ ์ฐฐ

๋Ÿฐํƒ€์ž„ ๋™์•ˆ์— ์‹คํ˜„๋˜๋Š” ๋‹คํ˜•์„ฑ์€ ๋Ÿฐํƒ€์ž„๋‹คํ˜•์„ฑ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, virtualํ‚ค์›Œ๋“œ, vtable์— ์˜ํ•ด ๊ตฌํ˜„๋œ๋‹ค. ๋Ÿฐํƒ€์ž„์— ์ •ํ•ด์ง€๋Š” ~์™€ ์ƒ๊ด€์—†์ด ์ผ๊ด€๋œ~~~~~~~~ ๋‹คํ˜•์„ฑ์€ ~์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ์ „์žฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. virtualํ•จ์ˆ˜๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ๋„ ๊ณ„์† virtualํ•จ์ˆ˜์ด๋‹ค.
  2. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ฐ€์ƒํ•จ์ˆ˜๋ฅผ vtable์˜ ํŠน์ • ์œ„์น˜๋ฅผ ์ฐธ๊ณ ํ•˜๋ผ๋Š” ๊ธฐ๊ณ„์–ด๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

2.1.1 virtualํ•จ์ˆ˜๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ๋„ ๊ณ„์† virtualํ•จ์ˆ˜์ด๋‹ค.

ํŠน์„ฑ ๋งด๋ฒ„ ํ•จ์ˆ˜์— virtualํ‚ค์›Œ๋“œ๊ฐ€ ์ง€์ •๋˜๋ฉด ์ด๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋“ค์€ virtualํ‚ค์›Œ๋“œ๋ฅผ ์ ์ง€ ์•Š์•„๋„ ํ•ด๋‹น ํ•จ์ˆ˜๋Š” virtualํ•จ์ˆ˜๊ฐ€ ๋จ

-> ์ด๋Ÿฌํ•œ ํŠน์„ฑ์ด ์žˆ์–ด์•ผ, ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์—์„œ๋„ ๊ฐ€์ƒํ•จ์ˆ˜๊ฐ€ ํ•ญ์ƒ ๊ฐ™์€ ์œ„์น˜์— ์žˆ๊ฒŒ ๋œ๋‹ค.

์˜ˆ์‹œ์—์„œ Baseํด๋ž˜์Šค์˜ listen1()๋Š” ๊ฐ€์žฅ ๋จผ์ € ์„ ์–ธ๋˜๋Š” virtual ํ•จ์ˆ˜์ด๋ฏ€๋กœ, vtable์˜ ์ฒซ๋ฒˆ์งธ ์ฃผ์†Œ์— ์œ„์น˜ํ•œ๋‹ค. ์ดํ›„ Base๋ฅผ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์—์„œ ๋ณ„๋„์˜ virtual ํ•จ์ˆ˜๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๊ณ , listen1()์€ ์˜์›ํžˆ vtable์˜ ์ฒซ๋ฒˆ์งธ ์œ„์น˜์— ์žˆ๊ฒŒ ๋œ๋‹ค.

2.1.2 ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ฐ€์ƒํ•จ์ˆ˜๋ฅผ vtable์˜ ํŠน์ • ์œ„์น˜๋ฅผ ์ฐธ๊ณ ํ•˜๋ผ๋Š” ๊ธฐ๊ณ„์–ด๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํŠน์ • ๋งด๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ธฐ๊ณ„์–ด๋กœ ๋ณ€ํ™˜ํ• ๋•Œ, ๋‹ค์Œ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๊ฒŒ ์–ด์…ˆ๋ธ”๋ฆฌ์–ด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค(in ์ปดํŒŒ์ผํƒ€์ž„).

๋”ฐ๋ผ์„œ, listen1()์„ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ณ€ํ™˜ํ• ๋•Œ, ์‹ค์ œ ํด๋ž˜์Šค์— ๋”ฐ๋ผ์„œ Baseํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋˜, ๊ทธ ์ž์‹ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋˜, ์—…์บ์ŠคํŒ…๋œ Base ํฌ์ธํ„ฐ์—์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋˜ ์ƒ๊ด€ ์—†๋‹ค. ๊ทธ๋ƒฅ ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” vtable๋กœ ๊ฐ€์„œ ์ฒซ๋ฒˆ์งธ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๋ผ๊ณ  ํ•˜๋ฉด ๋œ๋‹ค.

vtable์ž์ฒด๋Š” ์ปดํŒŒ์ผ ํƒ€์ž„์—์„œ ๊ฒฐ์ •๋˜์ง€๋งŒ, ์–ด๋–ค vtable์„ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜๋Š”์ง€๋Š” ๋Ÿฐํƒ€์ž„์— ํฌ์ธํ„ฐ์— ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ํ• ๋‹น๋˜๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.

์ด๋ ‡๊ฒŒ ์ผ๊ด„๋œ ๋ฐฉ์‹์œผ๋กœ vtable์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ ๋‹คํ˜•์„ฑ์ด ๊ตฌํ˜„๋œ๋‹ค.