C++知识记录(1)
友元
https://baike.baidu.com/item/友元/6150794?fr=aladdin
一种定义在类外部的普通函数或类,但它需要在类体内进行说明,为了与该类的成员函数加以区别,在说明时前面加以关键字friend。友元不是成员函数,但是它可以访问类中的私有成员。
#include<iostream> #include<cmath> using namespace std; class Point { public: Point(double xx, double yy) { x = xx; y = yy; }; void Getxy(); friend double Distance(Point &a, Point &b); private: double x, y; }; void Point::Getxy() { cout << "(" << x << "," << y << ")" << endl; } double Distance(Point &a, Point &b) { double dx = a.x - b.x; double dy = a.y - b.y; return sqrt(dx*dx + dy*dy); } int main(void) { Point p1(3.0, 4.0), p2(6.0, 8.0); p1.Getxy(); p2.Getxy(); double d = Distance(p1, p2); cout << "Distance is" << d << endl; return 0; }
引用&
- 通过引用,在函数调用的过程中,会实际修改指针的指向。改变指针这样会引入一个非法引用。
- system函数需要包括cstdlib库
#include<iostream> #include <cstdlib> using namespace std; void function2(int* &p) { int x = 12; p = &x; cout << "p: " << p << endl; cout << "*p: " << *p << endl; }int main() { int* a = new int; int b = 1; *a = b; cout << "a:" << a << endl; cout << "*a:" << *a << endl; function2(a); cout << "a:" << a << endl; cout << "*a:" << *a << endl; delete a; system("pause"); }
指针占用的地址空间大小相同
有32位和64位的区别
函数模板和类模板
类模板只能用class
指定默认
常量指针和指针常量
函数模板的调用
常用显式
类模板的成员函数创建
调用时候创建
Le vent se lève! . . . il faut tenter de vivre!
Le vent se lève! . . . il faut tenter de vivre!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2019-08-02 Thinkphp5.0快速入门笔记(1)