
C++语言
jltxgcy
热爱Android,热爱开源。
展开
-
C++ shared_ptr week_ptr unique_ptr
一、shared_ptrmain.cpp#include <iostream>using namespace std;int main(){ shared_ptr<int> myp(new int(100)); shared_ptr<int> myp1(myp); myp = nullptr;//效果同reset一样 //myp.re...原创 2020-02-13 19:49:40 · 664 阅读 · 0 评论 -
C++ 左值 右值 移动构造函数 移动赋值运算符
一、左值、右值、左值引用&、右值引用&&main.cpp#include <iostream>using namespace std;int main(){ string str = "I love china"; const char *p = str.c_str(); cout << (void*)p << e...原创 2020-02-13 19:34:16 · 1178 阅读 · 0 评论 -
C++ lambda表达式
一、lambda函数main.cpp#include <iostream>#include "template.h"using namespace std;int main(){ auto f5 = [](int a) ->int { return a + 1; }; f5(1); vector<int> myvector...原创 2020-02-07 19:38:24 · 257 阅读 · 0 评论 -
C++ std::funtion 可调用函数 std::bind
一、std::funtiontemplate.h#pragma once#include <iostream>#include <map>#include <functional>class RC{public: void operator()(int tv) { std::cout << "RC operator:"...原创 2020-02-07 11:58:04 · 276 阅读 · 0 评论 -
C++ decltype用法
一、decltypedecltype用于推断类型template.h#pragma once#include <iostream>#include <map>template<typename T>class CT2{public: //typename T::iterator iter; decltype(T().begin()...原创 2020-02-07 10:27:58 · 1023 阅读 · 0 评论 -
C++ 可变参函数模板 可变参类模板 模板模板参数
一、可变参函数模板template.h#pragma once#include <iostream>#include <map>void myfunct2(){ std::cout << "终止调用" << std::endl;}template <typename T, typename...U>void ...原创 2020-02-07 08:08:45 · 435 阅读 · 0 评论 -
C++ 类模板、函数模板全特化、偏特化
一、类模板全特化、偏特化#pragma once#include <iostream>#include <map>template <typename T, typename U>class TC{public: TC() { std::cout << "泛化版本构造函数" << std::endl; }...原创 2020-02-06 20:44:06 · 4061 阅读 · 0 评论 -
C++ using新用法、类成员指针
一、using新用法template.h#pragma once#include <iostream>#include <map>template <typename wt>struct map_s{ typedef std::map<std::string, wt> type;};//C++11template &l...原创 2020-02-06 19:40:02 · 1012 阅读 · 0 评论 -
C++ 函数模板、类模板、类成员函数模板
一、函数模板template.h#pragma once#include <iostream>#include <map>template <typename T>T funcadd(T a, T b){ T addhe = a + b; return addhe;}template <typename T, int a, ...原创 2020-02-06 19:04:10 · 1275 阅读 · 0 评论 -
C++ const用法总结
一、const描述常量const int a = 100;//a不可以改变二、const描述引用int a = 10;const int& b = a;//b不可以改变a的值const int a = 10;const int & b = a;//只能用const int&类型引用三、const描述指针#include <iostre...原创 2020-02-06 11:28:53 · 278 阅读 · 0 评论 -
C++ 友元函数 友元类 友元成员函数
一、友元函数 fun.h#pragma once#include <iostream>#include "men.h"void fun(Men & men);fun.cpp#include "fun.h"void fun(Men & men){ men.function();}Men.h#pragma once#in...原创 2013-11-06 12:31:26 · 1631 阅读 · 0 评论 -
C++::使用说明
一、整体代码 01.cpp#include #include using namespace std;int var = 9;int main(void){ int var = 10; cout<<var<<endl;//返回局部变量10 cout<<::var<<endl;//返回全局变量9 return 0;} 二原创 2013-10-31 18:16:10 · 1281 阅读 · 0 评论 -
C++ 虚继承 多重继承
一、整体代码 01.cpp#include using namespace std;class Furniture{public: Furniture(int weight) : weight_(weight) { cout<<"Furniture ..."<<endl; } ~Furniture() {原创 2013-12-20 10:51:20 · 1588 阅读 · 0 评论 -
C++ 继承 构造函数、拷贝构造函数
一、整体代码 01.cpp#include using namespace std;class ObjectB{public: ObjectB(int objb) : objb_(objb) { cout<<"ObjectB ..."<<endl; } ~ObjectB() { cout<<"~Obje原创 2013-12-18 17:50:59 · 2268 阅读 · 0 评论 -
关于C++对象模型的思考
一、我们首先看一个例子,一个孙子类继承了两个父亲类,两个父亲类同时继承同一个爷爷类。#include using namespace std; class Parent{public: int p_; // p将会被所有的子类继承,也将是二义性的根源 Parent(int p)原创 2014-05-28 15:24:40 · 1470 阅读 · 0 评论 -
C++ 单例模式
一、整体代码 01.cpp#include using namespace std;class Singleton{public: static Singleton* GetInstance() { if (instacne_ == NULL) { instacne_ = new Singleton原创 2013-11-04 10:51:43 · 1524 阅读 · 0 评论 -
C++ 空类默认产生的成员
一共有6个:Empty(); // 默认构造函数Empty( const Empty& ); // 默认拷贝构造函数~Empty(); // 默认析构函数Empty& operator=( const Empty& ); // 默认赋值运算符Empty* operator&();原创 2013-11-01 17:04:35 · 1164 阅读 · 0 评论 -
C++ 拷贝构造函数
一、整体代码 Test.h#ifndef _TEST_H_#define _TEST_H_class Test{public:// 如果类不提供任何一个构造函数,系统将为我们提供一个不带参数的// 默认的构造函数 Test();explicit Test(int num); Test(const Test& other);void Display()原创 2013-11-01 16:04:52 · 1356 阅读 · 0 评论 -
C++ 智能指针
本文参考http://blog.youkuaiyun.com/luoshengyang/article/details/6786239一、RefBash.hclass RefBase{public: void incStrong(const void* id) const; void decStrong(const vo原创 2014-01-21 14:49:29 · 1978 阅读 · 0 评论 -
C++ 继承中 方法的使用
一、整体代码 01.cpp#include <iostream>using namespace std;class Base {public: virtual void display() { //虚函数,virtual声明的函数,向上转型后的对象才能调用到子类同名的方法 cout << "Base" << endl; ...原创 2013-12-18 18:29:56 · 1670 阅读 · 0 评论 -
C++ 异常处理
一、整体代码#include #include using namespace std;class MyException{public: MyException(const char* message) : message_(message) { cout<<"MyException ..."<<endl; } MyException(const MyExce原创 2014-01-17 17:47:39 · 1387 阅读 · 0 评论 -
C++ 结构体内存对齐
一、整体代码 01.cpp#include #include using namespace std; #pragma pack(2) //编译器预设偏移值为2 struct Test { char a;//偏移为0,占位1 double b;//偏移为8和2中最小数,为2,占2-9 char c;//偏移为1和2中最小数,为1,占10,最后结构原创 2013-10-31 18:14:46 · 1256 阅读 · 0 评论 -
C++ 虚析构函数用法
一、整体代码#include using namespace std;class IDemo{ public: virtual ~IDemo()//虚析构函数 { cout << "IDemo" << endl; }; virtual void OverrideMe() = 0;};原创 2013-12-18 19:21:36 · 1258 阅读 · 0 评论 -
C++ 构造函数初始化列表
一、整体代码 Object.h#ifndef _Object_H_#define _Object_H_class Object{public:enum E_TYPE { TYPE_A =100, TYPE_B =200 }; public:// 如果类不提供任何一个构造函数,系统将为我们原创 2013-11-01 16:03:55 · 1428 阅读 · 0 评论 -
C++ operator new placement new
一、整体代码 01.cpp#include#include usingnamespace std;class Test{public: Test(int n) : n_(n) { cout<<"Test(int n) : n_(n)"<<endl; } Test(const Test& other) {原创 2013-11-06 12:31:50 · 1436 阅读 · 0 评论 -
C++ 类型转换运算符
一、整体代码 Interger.h#ifndef _INTEGER_H_#define _INTEGER_H_class Integer{public: Integer(int n); ~Integer(); Integer& operator++(); //friend Integer& operator++(I...原创 2013-11-06 12:49:05 · 1487 阅读 · 0 评论 -
C++ Integer ++运算符重载
一、整体代码 Integer.h#ifndef _INTEGER_H_#define _INTEGER_H_class Integer{public: Integer(int n); ~Integer(); Integer(const Integer& other);//拷贝构造函数 Integer& operator++();原创 2013-11-06 12:31:34 · 2061 阅读 · 0 评论 -
C++ 数据库操作
一、整体代码 01.cpp#include using namespace std;class DBHelper{public: DBHelper() { cout<<"DB ..."<<endl; } ~DBHelper() { cout<<"~DB ..."<<endl; }原创 2013-11-06 12:32:02 · 2318 阅读 · 0 评论 -
C++ static 以及类的大小
一、整体代码 CountedObject.h#ifndef _COUNTED_OBJECT_H_#define _COUNTED_OBJECT_H_class CountedObject{public: CountedObject(); ~CountedObject();public: static int GetCount();priv原创 2013-11-01 18:12:23 · 1862 阅读 · 0 评论 -
C++ 内联函数
一、什么时候用内联函数 当代码较短,使用频率较高,程序频繁调用所花费时间较多,这时候定义内联函数。 内联函数每次调用都要嵌入整个代码中,增大了程序的大小,以空间换取了时间,如果内联函数太大,编译器不会认为是内联函数二、代码 inline int max(int a, int b){ return a>b?a:b;}原创 2013-10-31 18:19:37 · 1247 阅读 · 0 评论 -
C++ 布尔值用法
一、整体代码 01.cpp#include #include using namespace std;int main(void){ bool b = 100; bool c = false; cout<<b<<endl;//运行结果为1 cout<<c<<endl;//运行结果为0 return 0;} 二、解释原创 2013-10-31 18:15:27 · 19429 阅读 · 0 评论 -
C++ 抽象类
一、整体代码 01.cpp#include using namespace std;class Shape{public: virtual void Draw() = 0;//纯虚函数 virtual ~Shape() {}//有向上转型,一般都申明成virtual};class Circle : public Shape{public: v原创 2013-12-18 19:01:08 · 1149 阅读 · 0 评论 -
C++ 只声明该类为抽象类
一、整体代码 01.cpp#include using namespace std;class IDemo{ public: virtual ~IDemo() = 0;//纯虚析构函数};class Child : public IDemo{ public: ~Child() {原创 2013-12-18 19:14:56 · 1827 阅读 · 0 评论 -
C++ 构造函数 析构函数
一、整体代码 Test.h#ifndef _TEST_H_#define _TEST_H_class Test{public: // 如果类不提供任何一个构造函数,系统将为我们提供一个不带参数的 // 默认的构造函数 Test(); Test(int num); void Display(); ~Test();pri原创 2013-11-01 10:27:13 · 1349 阅读 · 0 评论 -
C++ const_cast,static_cast、dynamic_cast、reinterpret_cast的关系
一、static_cast相关类型转换:double f = 13.14f;int i = static_cast<int> f;父类转子类:子类* p2 = static_cast<父类*>(p);二、const_cast不能去除常量的常量性:const int i = 100;int i = const_cast<int&...原创 2014-05-27 09:47:38 · 1942 阅读 · 0 评论 -
C++ const方法及对象
一、整体代码 01.cpp#include <iostream>using namespace std;class Test{public: Test(int x) : x_(x), outputTimes_(0) { } int GetX() const//不能改变变量的值 { cout<...原创 2013-11-01 17:36:10 · 1418 阅读 · 0 评论 -
C++ 引用用法
一、整体代码 01.cpp#include <iostream> #include <stdio.h> using namespace std; int main(void) { int val = 1024; int& refval = val;//引用必须初始化,不能只声明 int ...原创 2013-10-31 18:22:16 · 1479 阅读 · 0 评论 -
C++ extern c 用法
一、整体代码 01.cpp#include #include #include "add.h" using namespace std; int main(void) { add(1,2); return 0; } add.cint add(int x, int y) { retu原创 2013-10-31 18:18:59 · 1422 阅读 · 0 评论 -
C++ 动态分配 结构体
一、C语言中结构体与指针,参考如下: http://blog.youkuaiyun.com/jltxgcy/article/details/17226723二、C++与C语言不同的地方在于动态分配结构体 C++推荐用new来代替malloc,delete来代替freestruct s_options *opts = new (struct s_optio原创 2014-01-14 17:26:53 · 4577 阅读 · 0 评论 -
C++ 转换构造函数 赋值语句
一、整体代码 Test.h#ifndef _TEST_H_#define _TEST_H_class Test{public: // 如果类不提供任何一个构造函数,系统将为我们提供一个不带参数的 // 默认的构造函数 Test(); /*explicit */Test(int num); void Display(); T原创 2013-11-01 10:56:52 · 1433 阅读 · 0 评论