![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
baidu_19552787
这个作者很懒,什么都没留下…
展开
-
C++ 基础智能指针 shared_ptr
// ConsoleApplication22.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "stdio.h"#include "iostream"#include "memory"using namespace std;class A{public: A(){ cout << "A==" << endl; } A(int a)原创 2021-10-13 14:49:12 · 82 阅读 · 0 评论 -
C++基础 new和delete要配对调用; new 类型[]和delete 对象[]要配对调用
class A{public: A(){ cout << "A类的构造函数会执行" << endl; } ~A(){ cout << "~A类的析构函数会执行" << endl; }};BOOL CMFCApplication2Dlg::OnInitDialog(){ CDialogEx::OnInitDialog(); // Set the icon for this dialog. The framework does this au原创 2021-10-12 14:57:35 · 156 阅读 · 0 评论 -
C++基础类模板
myVector.h#pragma once#include "iostream"using namespace std;//-- 类模板的模板参数 分为类型模板参数和 非类型模板参数template<typename T, typename K, int size, int tSize = 1>class MyVector{public: MyVector(); ~MyVector(); //-- 在类模板内部,可以直接使用类模板名 MyVector,加不加模板参数<原创 2021-10-07 12:07:01 · 93 阅读 · 0 评论 -
C++ 基础函数模板、模板的 模板参数列表、模板参数列表中的类型模板参数 与 非类型模板参数
// ConsoleApplication20.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;//-- <>尖括号内是模板参数列表,模板参数列表中可以定义 类型参数,在这里T就是类型模板参数template<typename T>T addNum(T a, T b){ retu原创 2021-09-26 21:47:15 · 1134 阅读 · 0 评论 -
C++基础成员函数指针与成员变量指针
// ConsoleApplication19.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;class TC{public: static int ms_i; int m_i; int m_ii; void normalFunc(int){ cout << "成员函数" <原创 2021-09-26 19:13:19 · 268 阅读 · 0 评论 -
C++基础类型转换构造函数和类型转换函数(类型转换运算符)
// ConsoleApplication18.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;#define SPECIALCALL//-- 类型转换函数 存在二义性问题,编译不过。所以加了 #deiine SPECIALCALL控制class TestInt{public: //-- 类型转换构造原创 2021-09-26 15:04:09 · 129 阅读 · 0 评论 -
C++基础 多重继承引发的 虚基类与虚继承(基类被虚继承 就称为虚基类?)
//-- 不采用虚基类的方式classA.h#pragma once#include "grand.h"class A :public Grand{public: A(int a, int b, int c); ~A(); void testTongMingHanShu(); int m_iTingMIngBianLiang;private:};A::A(int a, int b, int c) :Grand(a){ printf("A的构造a= %d\n", a);}原创 2021-09-26 11:58:40 · 86 阅读 · 0 评论 -
C++ 基础多重继承
classA.h#pragma onceclass A{public: A(int i, int j, int k=1); ~A(); void testA();private:};classA.cpp#include "stdafx.h"#include "iostream"#include "classA.h"using namespace std;A::A(int i, int j, int k){ cout << "类A的构造函数" <<原创 2021-09-26 09:26:53 · 91 阅读 · 0 评论 -
C++基础左值和右值。 移动构造和移动赋值函数
classB.h#pragma onceclass B{public: B(); B(const B& b); ~B(); int m_bm;private:};classB.cpp#include "stdafx.h"#include "classB.h"#include "iostream"using namespace std;B::B() :m_bm(100){ //cout << "类B的构造函数被调用" << endl;原创 2021-09-25 21:14:18 · 123 阅读 · 0 评论 -
C++ 基础 友元函数、友元类、友元成员函数。以及两个头文件相互包含导致的超前引用问题
Men.h#pragma once#include "iostream"class A{ //-- 声明类B是类A的友元类,则B可以访问类A的所有成员了。 friend class B;private://public: int data;};class B{public: void callBAF(int i, A& a){ a.data = i; std::cout << "a.data=" << a.data << st原创 2021-09-15 23:17:29 · 486 阅读 · 0 评论 -
C++基础 封装 继承 多态、 父类的final关键字、子类的override关键字、构造 拷贝构造 析构的调用
Human.h#ifndef __HUMAN__#define __HUMAN__class HuMan{public: HuMan(char n[20], int a); //-- 一旦一个类 想要做父类,务必把这个类的析构函数写成虚函数。这样就能保证delete父类指针时能够调用正确的析构函数(即利用多态,先调用子类,再调用父类的析构函数) //-- 从而保证父类指针指向的子对象 内存被正确的释放。 virtual ~HuMan(); char name[20]; int age原创 2021-09-14 12:58:26 · 302 阅读 · 0 评论 -
C++基础 拷贝构造、运算符重载
Time.h#pragma once#include "iostream"#include "ExTimer.h"class Timer{public: Timer(); //-- 使用上的日常习惯: 单参构造,一般声明为显式的,拷贝构造一般不声明为explicit explicit Timer(int t); Timer(int h, int s, int m); ~Timer(); /* 叫做拷贝构造函数的构造函数:类的构造函数的第一个参数是类 类型的引用,假若有额外的参数,那原创 2021-09-14 00:29:26 · 79 阅读 · 0 评论 -
C++ 基础成员函数、显式的单参构造函数、c++11构造的特殊写法 const static mutable
Time.h#ifndef __MYTIME__#define __MYTIME__//#pragma once#include "iostream"#include "Player.h"using namespace std;class Time{public: //-- 成员函数的声明 void myPublicFunc(); Time(int hour, int min, int sec); //-- 单参构造,存在隐式类型转换的问题。在重载的构造函数中,将构造函数原创 2021-09-13 17:36:55 · 124 阅读 · 0 评论 -
C++基础多个.cpp文件,包含头一个.h文件 导致的重定义问题
myProject.h/*头文件声明: 防止单一 一个.cpp文件 在引用头文件时 会重复包含这个头文件。情况一:(比如a.cpp在 #include "myProject" 头文件的同时,又引入了一个 包含#include "myProject"的头文件, 在没有头文件卫士的情况下,会重复引入了myProject头文件,myProject头文件被引入了二次)当我们加入了头文件卫士后,把它分别引入在两个不同的app文件中, myFunc1.cpp 和 myFunc2.cpp中。在链接时 头文件中原创 2021-09-11 11:28:15 · 3241 阅读 · 0 评论 -
C++ 模板类vector<int> vi,以及模板迭代器vector<int>::iterator p = vi.begin();
// ConsoleApplicationC++jichu9.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"#include "string"#include "vector"using namespace std;/*字符串C中 char str[] {"Im ok!"};c++ string*///-- 字符数组的常用操作原创 2021-09-10 21:11:06 · 369 阅读 · 0 评论 -
C++基础常量指针与指针常量,数组指针与指针数组,const &作为函数形参可接受三种多样化的实参
// ConsoleApplicationC++jichu8.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;/*常量指针和指针常量常量指针: const char *p; 指针p指向的内容不可通过p来改变,指针本身可以变指针常量: char * const p; 指针p指向的内容可通过p来改变,指针本原创 2021-09-10 11:01:21 · 116 阅读 · 0 评论 -
C++基础函数指针、数组指针 、const
// ConsoleApplicationC++jichu6.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;//-- 输出不同类型的地址void putAdress(){ //-- 输出字符类型的地址是一个字符串 char cc = 'a'; //printf(&cc); //getcha原创 2021-09-04 14:55:16 · 72 阅读 · 0 评论 -
C++基础静态局部变量、字符串、数组、引用(写一个不需要传参,利用引用和静态局部变量进行计数和还原的 函数)
// ConsoleApplicationC++jichu5.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;void swap(int &a, int&b){ int t = a; a = b; b = t;}//-- 不传入参数的计数器,并且能够清0//-- 返回的是地址in原创 2021-09-03 13:07:27 · 207 阅读 · 0 评论 -
C++基础函数、内存管理、结构体对象按照对象某个成员进行排序
// ConsoleApplicationC++jichu4.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream"using namespace std;struct Date{ int year; int month; int day; void print(){ cout << year << ' ' <&原创 2021-09-03 12:00:53 · 103 阅读 · 0 评论 -
C++ 基础类型转换。最好不用强转,必须要转用以下4种
// ConsoleApplicationC++jichu3.cpp : Defines the entry point for the console application.//#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){ //-- static_cast int ia = static_cast<int>(12.346); int b = 100; void * p = &b; int *原创 2021-09-02 22:16:32 · 187 阅读 · 1 评论 -
C++ 基础命名空间、结构体、枚举、联合、头文件
// ConsoleApplicationC++jichu1.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "cstdio" //可用c语言的标准库头文件#include "iostream"using namespace std; //标准库中的所有名字都放入一个名叫std的空间中。//-- 定义自己的命名空间namespace myProjName{ int原创 2021-09-02 21:43:28 · 173 阅读 · 0 评论 -
C++ 打印出每个字节的二进制
#include "stdafx.h"#include "iostream"using namespace std;//-- 依次打印出每个二进制位void showBits(int var){ //char 和 unsigned char 没有什么不同都是一个字节,唯一的区别是char的最高位是符号位-128~127,unsigned char 是0~255 typedef unsigned char uc; uc * p = (uc*)&var; printf("%s", ".原创 2021-09-02 10:58:50 · 2167 阅读 · 0 评论 -
C++类型符号位
// ConsoleApplicationTestCharOrUnsignedChar.cpp : Defines the entry point for the console application.//#include "stdafx.h"void testBits(unsigned char v){ //char 和 unsigned char 都是一个字节,唯一的区别的char有符号位-128~127 unsigned char是0~255 char c = v; unsigne原创 2021-09-02 10:14:30 · 389 阅读 · 0 评论 -
C++ decltype的使用
// ConsoleApplicationDecltype.cpp : Defines the entry point for the console application.///*decltype类型说明符,返回操作数的类型数据,在此过程中,编译器分析表达式并得到它的类型,却不实际计算表达式的值*/#include "stdafx.h"#include "typeinfo"//#include "stdio.h"#include "iostream"using namespace s原创 2021-09-01 13:32:52 · 308 阅读 · 0 评论 -
C++智能指针unique_ptr使用之二
// ConsoleApplicationUnique_ptr2.cpp : Defines the entry point for the console application.///*智能指针 unique_ptr 使用和shared_ptr不同,可以有多个shared_ptr指向同一个内存,只能有1个unique_ptr指向某个内存。因此unique_ptr不支持普通的拷贝和赋值。一,先来个表格,唠唠unique_ptr操作 功能描述unique_ptr<T> u(q)原创 2021-08-31 13:10:22 · 101 阅读 · 0 评论 -
C++ 智能指针unique_ptr使用之一
// ConsoleApplicationUnique_ptr.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "stdio.h"#include "iostream"//-- unique_ptr是一种定义在头文件<memory>中的智能指针#include <memory>using namespace std;//-- 必须在原创 2021-08-30 20:43:25 · 155 阅读 · 0 评论 -
C++宏定义
编译器内置宏定义:__DATE__:表示当前日期的字符串,格式为month/day/year(月/日/年)__TIME__:表示当前时间,格式为hour:minute:second(时:分:秒)__FILE__:表示正在处理的当前文件名字字符串__LINE__:表示正在处理的当前行号__FUNCTION__:表示正在处理的函数名__STDC__:表示编译器是否遵循ANSI C标准,如果是,它就是个非零值#include <stdio.h> int main(){ prin原创 2021-08-30 10:03:23 · 320 阅读 · 0 评论 -
C++ 关键字typedef
typedef 创建别名,隐藏笨拙 难以理解的语法用法1:typedef size int;std::vector<size> vec;用法2:掩饰复合类型,如指针和数组typedef char Line[8];Line 就表示一个char[8]类型。可以这样使用 Line a,b;用法3:同样可以隐藏指针语法typedef se::Object* se_object_ptr;se_object_ptr 就表示 se::Object* 类型,使用se::Object原创 2021-08-18 12:26:06 · 71 阅读 · 0 评论 -
C++ final关键字
一. final 一个类或结构体不希望被继承class Base final{};//-- 错误Base不能被继承class Derive: public Base{};二. final 一个虚函数不能被覆盖(override)class Base{ public: virtual void aFunc() final{}};class Derive:public Base{ public: //-- 错误 基类虚函数不能被继承 virtual void aFunc()原创 2021-08-15 10:56:01 · 193 阅读 · 0 评论