Le vent se lève, il faut ten|

大浪淘沙、

园龄:7年1个月粉丝:49关注:19

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;
}

image

引用&

  • 通过引用,在函数调用的过程中,会实际修改指针的指向。改变指针这样会引入一个非法引用。
  • 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");
}

image

指针占用的地址空间大小相同

有32位和64位的区别
image

函数模板和类模板

image
image
类模板只能用class
image
image
指定默认
image

常量指针和指针常量

image
image

函数模板的调用

常用显式
image

类模板的成员函数创建

调用时候创建
image

posted @   大浪淘沙、  阅读(48)  评论(0编辑  收藏  举报
历史上的今天:
2019-08-02 Thinkphp5.0快速入门笔记(1)
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示