⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 03-1.cpp

📁 effective stl 源代码 code
💻 CPP
字号:
//
// Example from ESTL item 3
//

#include <iostream>
#include <vector>
#include <algorithm>
#include "ESTLUtil.h"
#include "Widget.h"

class SpecialWidget:			// SpecialWidget inherits from Widget
	public Widget
{
public:
	void draw(double ScaleFactor = 1) const;
};					

void SpecialWidget::draw(double ScaleFactor) const
{
	std::cout << "Drawing Special Widget (val = " << val << ") using ScaleFactor " <<
			ScaleFactor << "..." << std::endl;
}

int main()
{
	using namespace std;
	using namespace ESTLUtils;

	vector<Widget> vw;
	vw.push_back(Widget());		// "Vanilla" Widget

	SpecialWidget sw;			// Special Widget
	vw.push_back(sw);			// sw is copied as a base class
								// object into vw. Its specialness
								// is lost during the copying.	

	vw[0].draw(3);				// IS a Widget, draws as one
	vw[1].draw(3);				// WAS a SpecialWidget, now just a Widget


	const size_t maxNumWidgets = 223;

	{
		Widget w[maxNumWidgets];	// create an array of maxNumWidgets
									// Widgets, default-constructing each one
	}

	{
		vector<Widget> vw; // create a vector with zero Widget
						   // objects that will expand as needed
	}

	{
		vector<Widget> vw;
		vw.reserve(maxNumWidgets); // see Item 14 for details on reserve
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -