码迷,mamicode.com
首页 > 编程语言 > 详细

C++虚函数的默认参数问题

时间:2014-08-13 22:33:47      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:虚函数的默认参数

#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;

class Base
{
public:
	Base(int i):m_num(i)
	{
		cout<<"Base Constructor"<<endl;
	}
	virtual ~Base()
	{
		cout<<"Base Deconstructor"<<endl;
	}

	virtual void func(int num=-100)
	{
		cout<<num<<endl;
		cout<<"This is Base class"<<endl;
	}
private:
	int m_num;
};

class Derived:public Base
{
public:
	Derived():Base(10)
	{
		cout<<"Derived constructor"<<endl;
	}
	~Derived()
	{
		cout<<"Derived deconstructor"<<endl;
	}
	void func(int num=100)
	{
		cout<<num<<endl;
		cout<<"This is Derived class"<<endl;
	}
};


int main()
{
	{
		Base* pb=new Derived();
		cout<<"-------------------------"<<endl;
		pb->func();
		cout<<"-------------------------"<<endl;
		Derived* pd=dynamic_cast<Derived*>(pb);
		pd->func();
		cout<<"-------------------------"<<endl;

		delete pb;
	}

	system("pause");
	return 0;
}

bubuko.com,布布扣

总结:

虚函数的默认参数,与调用该虚函数的指针类型对应

C++虚函数的默认参数问题,布布扣,bubuko.com

C++虚函数的默认参数问题

标签:虚函数的默认参数

原文地址:http://blog.csdn.net/cjc211322/article/details/38541925

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!