随笔分类 -  C/C++

摘要:虚函数 类的对象模型 class A{ public: void f1(){} void f2(){} virtual void f3(){} virtual void f4(){} }; class B{ public: void f1(){} void f2(){} }; class C{ }; 阅读全文
posted @ 2024-03-18 17:06 拾墨、 阅读(21) 评论(0) 推荐(0)
摘要:左值和右值 一个值如果能取地址就是左值,反之就是右值 int a = 10 //a是左值,10是右值 左值引用 左值引用只能绑定左值 int l = 10; int& a = l; //正确 int& a = 10; //错误,10是右值,不能被左值引用绑定 const int& a = 10; / 阅读全文
posted @ 2024-03-14 21:19 拾墨、 阅读(9) 评论(0) 推荐(0)
摘要:1. 线程库的使用 创建进程 # include<iostream> # include<thread> //线程库头文件 using namespace std; void thread_1() { cout<<"子线程1"<<endl; } int thread_2(int x) { cout< 阅读全文
posted @ 2024-03-08 14:24 拾墨、 阅读(21) 评论(0) 推荐(0)
摘要:引入 lambda表达式也有人称之为匿名函数,能够在任何作用域下快速定义一个函数 下面这行代码就是一个最简单的lambda表达式,最后输出为3 auto f = [](int x , int y)->int{return x+y;}; cout<<f(1,2); 我们来解析一下lambda表达式 a 阅读全文
posted @ 2024-03-07 20:21 拾墨、 阅读(13) 评论(0) 推荐(0)
摘要:函数指针 int f(int x,int y) { return x*x+y*y; } int main() { int (*p) (int , int ); //括号里也可以写成(int x , int y) p = f; std::cout<<p(1,2); //输出为5 return 0; } 阅读全文
posted @ 2024-03-07 16:17 拾墨、 阅读(15) 评论(0) 推荐(0)
摘要:运算符重载本质 重新定义运算符的操作,返回自定义的结果。 对于A operator sign (B res1 , C res2) B类型的res1和C类型的res2,进行sign操作,返回一个类型是A的结果。 1. 一元运算符重载 (1)重载++ class student { public: in 阅读全文
posted @ 2024-02-06 20:01 拾墨、 阅读(30) 评论(0) 推荐(0)
摘要:1. 命名空间定义 注:命名空间只能在全局变量中定义 namespace mystd { int x,y; int max(int a,int b) { return std::max(a,b); } } int main() { int a = 10,b = 4; std::cout<<mystd 阅读全文
posted @ 2024-02-06 14:12 拾墨、 阅读(10) 评论(0) 推荐(0)