打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C -入门语法(五)
仿函数(函数对象)
  • 仿函数:将一个对象当作一个函数一样来使用
  • 对比普通函数,它作为对象可以保存状态
#include <iostream>using namespace std;//int sum(int a, int b) {//	return a   b;//}class Sum {	int m_age;public:	Sum(int age) :m_age(age) { }	int operator()(int a, int b) {		if (this->m_age > 10) {		}		else {		}		return a   b;	}};class Point {	friend ostream &operator<<(ostream &, const Point &);public:	int m_x;	int m_y;	Point(int x, int y) :m_x(x), m_y(y) { }};// output streamostream &operator<<(ostream &cout, const Point &point) {	return cout << "(" << point.m_x << ", " << point.m_y << ")";}// input streamistream &operator>>(istream &cin, Point &point) {	return cin >> point.m_x >> point.m_y;}int main() {	Point p1(10, 20);	cin >> p1;	cout << p1 << endl;	// Sum sum(20);	// cout << sum(10, 20) << endl;	// cout << sum.operator()(10, 20) << endl;	getchar();	getchar();	return 0;}
运算符重载注意点
  • 有些运算符不可以被重载,比如
    • 对象成员访问运算符:
    • 域运算符:::
    • 三目运算符:?:
    • sizeof
  • 有些运算符只能重载为成员函数,比如
    • 赋值运算符:=
    • 下标运算符:[ ]
    • 函数运算符:( )
    • 指针访问成员:->
模板(template)
  • 泛型,是一种将类型参数化以达到代码复用的技术,C 中使用模板来实现泛型
  • 模板的使用格式如下
    • template <typename\class T>
    • typename和class是等价的
  • 模板没有被使用时,是不会被实例化出来的
  • 模板的声明和实现如果分离到.h和.cpp中,会导致链接错误
  • 一般将模板的声明和实现统一放到一个.hpp文件中
模板-Array
#include <iostream>using namespace std;template <class Item>class Array {	friend ostream &operator<<<>(ostream &, const Array<Item> &);	int m_size = 0;	int m_capacity = 0;	Item *m_data = NULL;public:	Array(int capacity);	~Array();	void add(Item value);	Item get(int index);	int size();	Item operator[](int index);	void display();};template <class Item>Array<Item>::Array(int capacity) {	if (capacity <= 0) return;	this->m_data = new Item[capacity]{};	this->m_capacity = capacity;}template <class Item>Array<Item>::~Array() {	if (!this->m_data) return;	delete[] this->m_data;	this->m_data = NULL;}template <class Item>void Array<Item>::add(Item value) {	if (this->m_size == this->m_capacity) {		// 扩容		cout << "数组已满" << endl;		return;	}	this->m_data[this->m_size  ] = value;}template <class Item>Item Array<Item>::get(int index) {	if (index < 0 || index >= this->m_size) return 0;	return this->m_data[index];}template <class Item>int Array<Item>::size() {	return this->m_size;}template <class Item>Item Array<Item>::operator[](int index) {	return get(index);}template <class Item>void Array<Item>::display() {	cout << "[";	for (int i = 0; i < this->m_size; i  ) {		cout << this->m_data[i];		if (i != this->m_size - 1) {			cout << ", ";		}	}	cout << "]" << endl;}template <class Item>ostream &operator<<<>(ostream &cout, const Array<Item> &array) {	cout << "[";	for (int i = 0; i < array.m_size; i  ) {		cout << array.m_data[i];		if (i != array.m_size - 1) {			cout << ", ";		}	}	return cout << "]";}
#include <iostream>#include "Array.hpp"using namespace std;//template <class Item> //class Array {//	// friend ostream &operator<<(ostream &, const Array &);//	int m_size = 0;//	int m_capacity = 0;//	Item *m_data = NULL;//public://	Array(int capacity) {//		if (capacity <= 0) return;////		this->m_data = new Item[capacity] {};//		this->m_capacity = capacity;//	}////	~Array() {//		if (!this->m_data) return;////		delete[] this->m_data;//		this->m_data = NULL;//	}////	void add(Item value) {//		if (this->m_size == this->m_capacity) {//			// 扩容//			cout << "数组已满" << endl;//			return;//		}//		this->m_data[this->m_size  ] = value;//	}////	Item get(int index) {//		if (index < 0 || index >= this->m_size) return 0;//		return this->m_data[index];//	}////	int size() {//		return this->m_size;//	}////	Item operator[](int index) {//		return get(index);//	}////	void display() {//		cout << "[";//		for (int i = 0; i < this->m_size; i  ) {//			cout << this->m_data[i];//			if (i != this->m_size - 1) {//				cout << ", ";//			}//		}//		cout << "]" << endl;//	}//};//ostream &operator<<(ostream &cout, const Array &array) {//	cout << "[";//	for (int i = 0; i < array.m_size; i  ) {//		cout << array.m_data[i];//		if (i != array.m_size - 1) {//			cout << ", ";//		}//	}//	return cout << "]";//}class Person {	friend ostream &operator<<(ostream &, const Person &);	int m_age;public:	Person(int age = 0) :m_age(age) { }};ostream &operator<<(ostream &cout, const Person &person) {	return cout << "age=" << person.m_age;}int main() {	/*Array<Person *> array(3);	array.add(new Person(11));	array.add(new Person(12));	array.add(new Person(13));	array.display();*/	Array<Person> array(3);	array.add(Person(11));	array.add(Person(12));	array.add(Person(13));	// array.display();	cout << array << endl;	/*Array<int> array(5);	array.add(11);	array.add(22);	array.add(33);	array.add(44);	array.add(55);	array.display();	Array<double> array2(3);	array2.add(10.8);	array2.add(10.9);	array2.add(10.4);	array2.display();*/	// cout << array << endl;	/*Array array;	array.add(10);	array.add(20);	array.add(11);	array.add(22);	array.get(2);	array[2];	array.size() == 4;	array.remove(3);	array.insert(1, 30);*/	getchar();	return 0;}
#include "Swap.h"//void swapValues(int &v1, int &v2) {//	int tmp = v1;//	v1 = v2;//	v2 = tmp;//}////void swapValues(double &v1, double &v2) {//	double tmp = v1;//	v1 = v2;//	v2 = tmp;//}
#pragma once// template <class T> void swapValues(T &v1, T &v2);template <class T> void swapValues(T &v1, T &v2) {	T tmp = v1;	v1 = v2;	v2 = tmp;}//void swapValues(int &v1, int &v2);//void swapValues(double &v1, double &v2);
#pragma oncetemplate <class T> void swapValues(T &v1, T &v2) {	T tmp = v1;	v1 = v2;	v2 = tmp;}
#include <iostream>#include "Swap.hpp"using namespace std;void test() {	double a = 20.8;	double b = 30.4;	swapValues(a, b); 	cout << "test() a = " << a << ", b = " << b << endl;}
类型转换
  • C语言风格的类型转换符
    • (type)expression
    • type(expression)
  • C 中有4个类型转换符
    • static_cast
    • dynamic_cast
    • reinterpret_cast
    • const_cast
    • 使用格式:xx_cast(expression)
#include <iostream>using namespace std;class Person {public:	int m_age;	virtual void run() { }};class Student : public Person {public:	int m_score;};class Car {};void test1const_cast() {	const Person *p1 = new Person();	Person *p2 = const_cast<Person *>(p1);	Person *p3 = (Person *)p1;	p2->m_age = 20;	p3->m_age = 30;	cout << p1->m_age << endl;}void test2dynamic_cast() {	Person *p1 = new Person();	Person *p2 = new Student();	/*Student *stu1 = (Student *) p1;	Student *stu2 = (Student *) p2;	Car *car = (Car *) p2;*/	Student *stu1 = dynamic_cast<Student *>(p1);	Student *stu2 = dynamic_cast<Student *>(p2);	Car *car = dynamic_cast<Car *>(p2);	cout << stu1 << endl;	cout << stu2 << endl;	cout << car << endl;}void test3static_cast() {	Person *p1 = new Person();	Person *p2 = new Student();	Student *stu1 = static_cast<Student *>(p1);	Student *stu2 = static_cast<Student *>(p2);	int i = 10;	double d = i;	cout << stu1 << endl;	cout << stu2 << endl;}int main() {	/*int a = 10;	double d1 = (double) a;	double d2 = double(a);	cout << d1 << endl;	cout << d2 << endl;*/	/*Person *p1 = new Person();	Person *p2 = new Student();	Student *stu1 = reinterpret_cast<Student *>(p1);	Student *stu2 = reinterpret_cast<Student *>(p2);	Car *car = reinterpret_cast<Car *>(p2);	cout << p1 << endl;	cout << p2 << endl;	cout << p2 << endl;	cout << stu1 << endl;	cout << stu2 << endl;	cout << car << endl;*/	//// 0a 00 00 00	//int i = 10;	//// 00 00 00 00 00 00 24 40	//double d = i;	//double d1 = 10.0;	//int i1 = d1;	// 0a 00 00 00	// int i = 10;	// 0a 00 00 00 cc cc cc cc 	// double d = reinterpret_cast<double&>(i);	// cout << i << endl;	// cout << d << endl;	/*int *p = reinterpret_cast<int *>(100);	int i = reinterpret_cast<int>(p);*/	//cout << "i = " << i << endl;	//cout << "d = " << d << endl;	//cout << sizeof(i) << endl;	//cout << sizeof(d) << endl;	getchar();	return 0;}
来源:https://www.icode9.com/content-1-524301.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C 技巧:二维动态数组类模板
c++中运算符的一些不同的使用 欧洲的有些需要这样
71、STL迭代器技术
运算符重载
C++运算符重载(2)
C++模板类中友元函数重载输出运算符
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服