虚析构

#include<iostream>
class Base
{
public:
Base() { std::cout << "base 构造" << std::endl; } virtual~Base() { std::cout << "base 析构" << std::endl; }
};
class Derived : public Base
{
public:
Derived() { std::cout << "derived 构造" << std::endl; }
~Derived() { std::cout << "derived 析构" << std::endl; }
}; int main()
{
Base* base = new Base();
delete base;
std::cout << "--------------" << std::endl;
Derived* der = new Derived();
delete der;
std::cout << "--------------" << std::endl;
Base* p = new Derived();
delete p;
std::cin.get();
}

联合体

#include<iostream>

struct Vector1
{
float x, y;
}; struct Vector2
{
union
{
struct
{
float x, y, z, w;
};
struct
{
Vector1 a, b; };
};
};
void PrintVector(const Vector1& vector)
{
std::cout << vector.x << " " << vector.y << std::endl;
}
int main()
{
Vector2 vector = { 1.0f,2.0f,3.0f,4.0f };
PrintVector(vector.a);
PrintVector(vector.b);
// 1 2 3 4
vector.z = 100.0f;
PrintVector(vector.a);
PrintVector(vector.b);
// 1 2 100 4
std::cin.get();
}

线程

#include<iostream>
#include<thread> static bool Finished = false; void DoWorker()
{
using namespace std::literals::chrono_literals;
std::cout << "id: " << std::this_thread::get_id() << std::endl;
while (!Finished)
{
std::cout << "Working...." << std::endl;
std::this_thread::sleep_for(1s);
//std::cin.get() }
} int main()
{
std::thread worker(DoWorker);
std::cin.get();
Finished = true;
worker.join(); std::cin.get();
}

预处理

#if _DEBUG==1
#define LOG(x) cout<<x<<endl
#elif(_RELESE)
#define LOG(x)
#endif

重载操作符

class Vector1
{
public:
float x, y;
public:
Vector1(float X, float Y)
: x(X), y(Y)
{
Vector1* const &e = this;
}
Vector1 Add(const Vector1& other)const
{
return Vector1(x + other.x, y + other.y);
}
Vector1 operator+(const Vector1& other)const
{
return Add(other);
}
Vector1 Multiply(const Vector1& other)const
{
return Vector1(x * other.x, y * other.y);
}
Vector1 operator*(const Vector1& other)const
{
return Multiply(other);
}
bool operator==(const Vector1& other)const
{
return x == other.x && y == other.y;
}
bool operator!=(const Vector1& other)const
{
return !(*this == other);
}
};
std::ostream& operator<<(ostream& stream, const Vector1& other)
{
stream << other.x << "," << other.y;
return stream;
}
int main()
{
Vector1 v1(1.1f, 2.2f);
Vector1 v2(3.3f, 4.4f);
Vector1 result1 = v1 + v2;
Vector1 result2 = v1 * v2;
cout << result1 << endl;
cout << result2 << endl;
if (result1 != result2)
cout << "==" << endl;
else
cout << "!=" << endl; cin.get();
}

智能指针

class Entity
{ };
{
shared_ptr<Entity>weakEneity;
{
unique_ptr<Entity>entity = make_unique<Entity>();
shared_ptr<Entity>sharedEntity = make_shared<Entity>();
weakEneity = sharedEntity;
entity->print();
} }

lambda

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std; void Foreach(const vector<int>& v,const function<void(int)>&func)
{
for (int i : v)
{
func(i);
}
}
int main()
{
vector<int>v = { 1,2,3,4 };
auto it = find_if(v.begin(), v.end(), [](int val){ return val > 2; });
cout << *it << endl; auto lambda = [=](int val) {cout << val << endl; };
Foreach(v, lambda);
cin.get();
}

函数指针

void Println(int val)
{
cout << val << endl;
} void Foreach(const vector<int>& v,void(*func)(int val))
{
for (int i : v)
{
func(i);
}
}
int main()
{
vector<int>v = { 1,2,3,4 };
Foreach(v, Println);
}
void Foreach(const vector<int>& v,void(*func)(int val))
{
for (int i : v)
{
func(i);
}
}
int main()
{
vector<int>v = { 1,2,3,4 };
Foreach(v, [](int val)
{
cout << val << endl;
});
}

使用箭头运算符,来获取内存中某个值的偏移量

#include<iostream>
using namespace std; struct Vector
{
int x, y, z;
}; int main()
{
int offset = (int)&((Vector*)nullptr)->x;
//x,y,z 执行结果分别是0,4,8
cout << offset << endl;
}

在不知道数组大小传递

template<int N>
void Array(const array<int,N>& arr)
{
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i];
}
}
int main()
{
array<int, 10>arr = { 1,1,1,1,1 };
Array(arr);
}

C++一些例子的更多相关文章

  1. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  2. C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)

    第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...

  3. REGEX例子

    作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...

  4. CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子

    CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...

  5. 简单例子了解View的事件分发

    什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...

  6. 简单的例子了解自定义ViewGroup(一)

    在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...

  7. kqueue例子

    网络服务器通常都使用epoll进行异步IO处理,而开发者通常使用mac,为了方便开发,我把自己的handy库移植到了mac平台上.移植过程中,网上居然没有搜到kqueue的使用例子,让我惊讶不已.为了 ...

  8. 今天有群友不是很清楚htm直接存数据库的危害,我简单举个例子

     通过这个案例就知道为什么不要把原生的html放数据库了  常见的几种转码  常用的几种显示方法 只有原生html和最下面一种弹框了,变成了持久xss 如果是Ajax的方式,请用@Ajax.JavaS ...

  9. ElasticSearch 5学习(5)——第一个例子(很实用)

    想要知道ElasticSearch是如何使用的,最快的方式就是通过一个简单的例子,第一个例子将会包括基本概念如索引.搜索.和聚合等,需求是关于公司管理员工的一些业务. 员工文档索引 业务首先需要存储员 ...

  10. 以实际的WebGIS例子探讨Nginx的简单配置

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 以实际项目中的一个例子来详细讲解Nginx中的一般配置,其中涉 ...

随机推荐

  1. acm数学总结

    1.给定两个质数,m, n, 大于n * m - n - m的数都可以被整数个n和m唯一组成. 相关习题:[Coins] (https://ac.nowcoder.com/acm/contest/34 ...

  2. 《深入理解Java虚拟机》(二) GC 垃圾回收机制

    @ 目录 一.概述 二.判断对象是否需要被回收方式 1.引用计数法: 2.可达性分析法: 三.垃圾收集算法 1.分代收集理论基础 2.标记-清除算法 3.复制-收集算法 4.标记-压缩(整理)算法 5 ...

  3. win10 wsl 运行后没有反应

    wsl 运行一段时间后执行没有反应, 需要重启LxssManager 管理员模式打开 powshell 找到pid, 结束pid >tasklist /svc /fi "service ...

  4. 项目实战:Qt终端命令模拟工具 v1.0.0(实时获取命令行输出,执行指令,模拟ctrl+c中止操作)

    需求   在Qt软件中实现部分终端控制命令行功能,使软件内可以又好的模拟终端控制,提升软件整体契合度.   Demo演示          运行包下载地址:   CSDNf粉丝0积分下载:https: ...

  5. RK3568开发笔记(二):入手RK3568开发板的套件介绍、底板介绍和外设测试

    前言   本篇主要介绍RK3568芯片和入手开发板的底板介绍以及开发板的外设.   开发板   笔者的开发板是全套+10.1寸屏. 开发板实物     开发板资源    开发版本提供资料     开发 ...

  6. python selenium list index out of range

    常见错误原因 常见错误原因 其他错误原因 场景 使用selenium循环打开并跳转到新的网页,然后关闭新的窗口,然后回到原来窗口,这时候获取list中的值,报错: list index out of ...

  7. 【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM

    问题描述 在Azure的Spring Cloud服务 (官名为:Spring Apps)中,在Metrics 页面中查看 App Memory Usage 和 jvm.memory.use,发现两则在 ...

  8. Glide源码解析四(解码和转码)

    本文基于Glide 4.11.0 Glide加载过程有一个解码过程,比如将url加载为inputStream后,要将inputStream解码为Bitmap. 从Glide源码解析一我们大致知道了Gl ...

  9. Java interface 接口的使用 implements 实现----

    1 package com.bytezreo.interfacetest; 2 3 /** 4 * 5 * @Description interface 接口的使用 implements 实现---- ...

  10. 4、 mysql的explain分析执行计划

    EXPLAIN或者 DESC命令获取 MySQL如何执行 SELECT 语句的信息,包括在 SELECT 语句执行过程中表如何连接和连接的顺序. 查询SQL语句的执行计划 : explain sele ...