![](https://img-blog.csdnimg.cn/20190918140213434.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
C++(98/03)
C++的1998/2003版本的基础语法学习记录
干锅土鸡
这个作者很懒,什么都没留下…
展开
-
动态内存申请的结果
#include <iostream>#include <new>#include <cstdlib>#include <exception>using namespace std;class Test{ int m_value;public: Test() { cout << "Test()" << endl; m_value...原创 2021-09-17 19:14:56 · 67 阅读 · 0 评论 -
函数异常规格说明
抛出来的字符型异常没有被处理#include <iostream>#include <cstdlib>#include <exception>using namespace std;void my_unexpected(){ cout << "void my_unexpected()" << endl; // exit(1); throw 1;}void func() throw(int)...原创 2021-09-17 18:01:52 · 67 阅读 · 0 评论 -
异常处理深度解析
#include <iostream>#include <cstdlib>#include <exception>using namespace std;void my_terminate(){ cout << "void my_terminate()" << endl; exit(1);//结束当前函数}class Test {public: Test() { c...原创 2021-09-17 16:33:28 · 93 阅读 · 0 评论 -
技巧:自定义内存管理
const成员函数内部不能修改成员变量!!!这个关键字在工程中用的比较少#include <iostream>#include <string>using namespace std;class Test{ int m_value; int * const m_pCount;//指针为常量,不能被改变,但是指向的值可以! /* mutable int m_count; */public: Test(int value = 0) .原创 2021-09-17 01:39:58 · 93 阅读 · 0 评论 -
拾遗:令人迷惑的写法
猜测:T是泛指类型,但是是类类型(之前是typename,现在直接限制死了:class)猜测错误!这里的class和typename作用一摸一样!!!#include <iostream>#include <string>using namespace std;template < class T >class Test{public: Test(T t) { cout << "t = " &l.原创 2021-09-16 23:20:12 · 86 阅读 · 0 评论 -
如何判断变量是指针/构造函数抛异常
基本类型可以识别,但是自定义类类型呢?不能识别核心思想:只匹配,不运行!!!这样就不会崩溃了#include <iostream>#include <string>using namespace std;class Test{public: Test() { } virtual ~Test() { }};template<typename T>char IsPtr(T* v) // ...原创 2021-09-15 23:27:22 · 70 阅读 · 0 评论 -
C++中的类型识别
不会报错,但有没有问题需要实际运行才行!#include <iostream>#include <string>using namespace std;class Base{public: virtual string type() { return "Base"; }};class Derived : public Base{public: string type() { r...原创 2021-09-15 13:28:25 · 117 阅读 · 0 评论 -
C++中的异常处理(下)
#include <iostream>#include <string>using namespace std;void Demo(){ try { try { throw 'c'; } catch(int i) { cout << "Inner: catch(int i)" << endl; ...原创 2021-09-15 00:08:12 · 305 阅读 · 1 评论 -
C++中的异常处理(上)
#include <iostream>#include <string>using namespace std;double divide(double a, double b){ const double delta = 0.000000000000001; double ret = 0; if( !((-delta < b) && (b < delta)) ) { ret =...原创 2021-09-14 18:49:41 · 102 阅读 · 0 评论 -
C语言异常处理
#include <iostream>#include <string>using namespace std;double divide(double a, double b, int* valid){ const double delta = 0.000000000000001; double ret = 0; if( !((-delta < b) && (b < delta)) ) { ...原创 2021-09-14 12:45:48 · 144 阅读 · 0 评论 -
单例类模板
#include <iostream>#include <string>using namespace std;class SObject{ //静态成员变量必须在类外初始化! static SObject* c_instance; SObject(const SObject&); SObject& operator= (const SObject&); SObject() { ...原创 2021-09-13 22:06:33 · 78 阅读 · 0 评论 -
智能指针类模板
#include <iostream>#include <string>#include <memory>using namespace std;class Test{ string m_name;public: Test(const char* name) { cout << "Hello, " << name << "." << endl; ..原创 2021-09-13 19:38:35 · 104 阅读 · 0 评论 -
数组类模板
编译通过,不会报错,但是这么写的话:int main(){ int a = 10; func<int,a>; return 0;}报错!!!模板参数在编译阶段就得被确定!以上在运行阶段才能确定参数a的值!#include <iostream>#include <string>using namespace std;template< typename T, int N >void func(){ T a[...原创 2021-09-13 00:06:49 · 530 阅读 · 0 评论 -
类模板深度剖析
#include <iostream>#include <string>using namespace std;template< typename T1, typename T2 >class Test{public: void add(T1 a, T2 b) { cout << "void add(T1 a, T2 b)" << endl; cout << a ...原创 2021-09-12 12:34:32 · 56 阅读 · 0 评论 -
类模板的概念和意义
#include <iostream>#include <string>using namespace std;template < typename T >class Operator{public: T add(T a, T b) { return a + b; } T minus(T a, T b) { return a - b; } T multipl...原创 2021-09-11 13:57:09 · 71 阅读 · 0 评论 -
深入理解函数模板
#include <iostream>#include <string>using namespace std;class Test{ //拷贝构造函数为私有,说明不能进行拷贝构造 Test(const Test&);public: Test() { }};template < typename T >void Swap(T& a, T& b){ T c = a; a =..原创 2021-09-11 13:19:31 · 71 阅读 · 0 评论 -
函数模板的概念和意义
#include <iostream>#include <string>using namespace std;#define SWAP(t, a, b) \do \{ \ t c = a; \ a = b; \ b = c; \}while(0)void Sw..原创 2021-09-11 00:18:50 · 65 阅读 · 0 评论 -
经典问题解析(四)
#include <iostream>#include <string>#include <cstdlib>using namespace std;class Test{ int* mp;public: Test() { cout << "Test::Test()" << endl; mp = new int(100); ...原创 2021-09-10 15:59:16 · 64 阅读 · 0 评论 -
被遗弃的多重继承
#include <iostream>#include <string>using namespace std;class BaseA{ int ma;public: BaseA(int a) { ma = a; } int getA() { return ma; }};class BaseB{ int mb;public: BaseB(int b)...原创 2021-09-09 23:56:12 · 89 阅读 · 0 评论 -
抽象类和接口
抽象类不能定义对象!加上关键字virtual,仅仅有函数的声明就可以了,就成为了抽象类#include <iostream>#include <string>using namespace std;class Shape{public: virtual double area() = 0;};class Rect : public Shape{ int ma; int mb;public: Rect(int a,...原创 2021-09-09 21:20:31 · 57 阅读 · 0 评论 -
对象模型分析(下)
对象模型分析(下)修改Derived类,增加构造函数和成员函数,编译结果大小不变!成员函数存放在代码段!如果编译器发现类中有虚函数,那么会强行塞一个指针成员变量(不可见但存在),此指针指向虚函数表给Demo中的print函数加上virtual关键字,再次打印结果:证实了存在指向虚函数表的指针#include <iostream>#include <string>using namespace std;class Demo{protected:原创 2021-09-09 00:29:57 · 98 阅读 · 0 评论 -
对象模型分析(上)
对象模型实际上就是探讨对象在内存中如何排布,包含成员变量、成员函数仅仅是访问权限的差别:class默认private,struct默认public原创 2021-09-07 23:23:53 · 325 阅读 · 0 评论 -
多态的概念和意义
#include <iostream>#include <string>using namespace std;class Parent{public: //这个关键字告诉编译器print()函数 //在后面继承时有可能会被重写 virtual void print() { cout << "I'm Parent." << endl; }};class Child : public Par...原创 2021-09-07 12:36:21 · 75 阅读 · 0 评论 -
同名覆盖的问题
#include <iostream>#include <string>using namespace std;class Parent{public: int mi; void add(int i) { mi += i; } void add(int a, int b) { mi += (a + b); }};class Child : public P.原创 2021-09-06 19:16:13 · 75 阅读 · 0 评论 -
父子间的冲突
上述代码编译通过,合法,子类中可以定义和父类同名的变量#include <iostream>#include <string>using namespace std;class Parent{public: int mi;};class Child : public Parent{public: int mi;};int main(){ Child c; c.mi = 100; // mi 究竟是子..原创 2021-09-05 22:31:03 · 62 阅读 · 0 评论 -
继承中的构造与析构
调用Child午餐构造函数,Child继承自Parent,创建对象c时,会调用父类无参构造函数,而父类没有无参构造函数,所以报错:no Parent constructor to call:#include <iostream>#include <string>using namespace std;class Parent {public: Parent() { cout << "Parent()" <...原创 2021-09-05 21:08:50 · 143 阅读 · 0 评论 -
不同的继承方式
#include <iostream>#include <string>using namespace std;class Parent{};class Child_A : public Parent{};class Child_B : protected Parent{};class Child_C : private Parent{};int main(){ return 0;}可正常编译,说明C++有很多种继承..原创 2021-09-04 19:30:59 · 125 阅读 · 0 评论 -
继承的概念和访问级别
#include <iostream>#include <string>using namespace std;class Parent{ int mv;public: Parent() { cout << "Parent()" << endl; mv = 100; } void method() { cout << "mv = ...原创 2021-09-02 22:33:02 · 100 阅读 · 0 评论 -
类型转换函数
小类型初始化给大类型是合法的原创 2021-08-31 16:46:06 · 70 阅读 · 0 评论 -
前置后置操作符
将程序反汇编:可以看出底层汇编一致,由于编译器的优化工程中i++,++i没有差异,这样子优化更加高效打印 1打印 0 1Test tt = t++; t++中的++重载后是++的原生语义,就是先赋值给别人再自己+1,仔细看重载函数可以发现的确是的。#include <iostream>#include <string>using namespace std;class Test{ int mValue;public: Test(i.原创 2021-08-30 20:25:10 · 75 阅读 · 0 评论 -
逗号操作符分析
#include <iostream>#include <string>using namespace std;void func(int i){ cout << "func() : i = " << i << endl;}int main(){ //(0,1,2):结果是2 //所以数组就赋值了前三个元素:2,5,8 //其他都初始化为0 //把()改成{ },就是正常的了 int a[3][3.原创 2021-08-29 21:13:05 · 71 阅读 · 0 评论 -
逻辑操作符陷阱
#include <iostream>#include <string>using namespace std;class Test{ int mValue;public: Test(int v) { mValue = v; } //为什么const修饰?因为下面的const对象只能调用const成员函数 //对象为了回去mValue的值,只能将这个成员函数定义为const int value().原创 2021-08-29 20:29:34 · 87 阅读 · 0 评论 -
智能指针
#include <iostream>#include <string>using namespace std;class Test{ int i;public: Test(int i) { this->i = i; } int value() { return i; } ~Test() { }};int main(){ for(int.原创 2021-08-26 22:04:56 · 66 阅读 · 0 评论 -
阶段性问题3
编译器默认提供赋值操作符重载实现,只不过是浅拷贝,深拷贝需要自己实现#include <iostream>#include <string>using namespace std;class Test{ int* m_pointer;public: Test() { m_pointer = NULL; } Test(int i) { m_pointer = new int(i); ..原创 2021-08-25 22:39:32 · 118 阅读 · 1 评论 -
函数对象分析
打印斐波那契数列#include <iostream>#include <string>using namespace std;class Fib{ int a0; int a1;public: Fib() { a0 = 0; a1 = 1; } Fib(int n) { a0 = 0; a1 = 1; .原创 2021-08-23 21:37:33 · 73 阅读 · 0 评论 -
数组操作符重载
类的对象怎么支持数组下标访问?原创 2021-08-22 10:31:28 · 66 阅读 · 0 评论 -
标准库中的重载/字符串类
有趣的重载:#include <stdio.h>const char endl = '\n';class Console{public: Console& operator << (int i) { printf("%d", i); return *this; } Console& operator << (char c) { prin原创 2021-08-21 22:07:52 · 92 阅读 · 0 评论 -
完善复数类
#ifndef _COMPLEX_H_#define _COMPLEX_H_class Complex{ double a; double b;public: Complex(double a = 0, double b = 0); double getA(); double getB(); double getModulus(); Complex operator + (const Complex& c); C..原创 2021-08-21 13:16:04 · 92 阅读 · 0 评论 -
操作符重载
#include <stdio.h>class Complex { int a; int b;public: Complex(int a = 0, int b = 0) { this->a = a; this->b = b; } int getA() { return a; } int getB() { retu.原创 2021-08-20 21:15:26 · 62 阅读 · 0 评论 -
类中的函数重载
#include <stdio.h>class Test{ int i;public: Test() { printf("Test::Test()\n"); this->i = 0; } Test(int i) { printf("Test::Test(int i)\n"); this->i = i; } Test(const..原创 2021-08-19 22:40:31 · 62 阅读 · 0 评论