静态联编与动态联编

1.什么是联编

将模板或者函数合并在一起生成可执行代码的处理过程(函数调用),按照联编进行的时间不同,可分为两种不同的联编方法:静态联编和动态联编。简单来理解,联编就是处理函数调用的过程

1)编译阶段进行的就是静态联编

2)程序运行阶段进行的就是动态联编

2.静态联编与动态联编的区别

1)静态联编适用于一般的函数调用,而动态联编适用于多态

2)C语言中的函数调用都是静态联编,多态联编只存在于C++的多态

3.动态联编的例子

#include <iostream>
using namespace std;

class Cfather
{
public:
	virtual void fun()
	{
		cout << "From Father" << endl;
	}
};

class Cson : public Cfather
{
public:
	void fun()
	{
		cout << "From Son" << endl;
	}
};


int main()
{
	Cfather *per;
	int a;
	cin >> a;

	switch (a)
	{
	case 0:
		per = new Cfather;
		break;
	case 1:
		per = new Cson;
		break;
	default:
		break;
	}

	per->fun();

	system("pause");
	return 0;
}

不过编译报错:

目前还不知道为什么??

猜你喜欢

转载自blog.csdn.net/qq_33757398/article/details/81417246