
C++_Knowledge
咖啡熊猫
newbee_____
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
C++陷阱:基类析构函数非虚,派生对象以基类指针析构
#include <iostream> #include <string> using std::string; using std::cout; using std::endl; class baseC { public: baseC():str("baseClass"){} ~baseC() { cout << str << end...原创 2019-01-16 16:12:01 · 569 阅读 · 0 评论 -
C++重载指针调用操作符示例
class realH { public: realH() :mem1(1) {} int Get() { return mem1; } void Set(int val) { mem1 = val; } private: int mem1; }; class pClass { public: pClass():pa(new realH()){} realH * operator-&...原创 2019-01-17 13:54:14 · 885 阅读 · 0 评论 -
C++ 非const转换为const 特例
一般情况下,非顶层const对象是允许被非const对象赋值(转换)的,但是今天发现一个特例,作为一个mark int main() { int a = 2; int * pa = &a; int ** ppa = &pa; /* const int ** cppa = &pa;*/ const int * cpa = &a; const int *...原创 2019-01-04 11:33:58 · 6986 阅读 · 2 评论 -
C++ dynamic_cast详解
转自Microsoft的官方文档, URL:https://docs.microsoft.com/zh-cn/cpp/cpp/dynamic-cast-operator?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(dynamic_cast...转载 2018-09-30 17:59:49 · 1363 阅读 · 0 评论 -
C++静态库与动态库
转载自:https://www.cnblogs.com/skynet/p/3372855.html 文章有一些浅显的语病及错误,但瑕不掩瑜,与大家共同学习。 C++静态库与动态库 这次分享的宗旨是——让大家学会创建与使用静态库、动态库,知道静态库与动态库的区别,知道使用的时候如何选择。这里不深入介绍静态库、动态库的底层格式,内存布局等,有兴趣的同学,推荐一本书《程序员的自我修养——链接、装载...转载 2018-09-27 17:37:23 · 162 阅读 · 0 评论 -
加深对sizeof的理解
#include <iostream> #include <vector> int main() { using std::cout; using std::endl; int x[10], *p = x; cout << sizeof(x) / sizeof(*x) << endl; cout << sizeof(p) &...原创 2018-09-05 19:32:16 · 204 阅读 · 0 评论 -
C++枚举(enum)的优雅用法
枚举等价于常量,这是常识,但是如果我知道了对应的常量,如何构造相应的枚举值呢 我以前的做法:if(1 == constVal)enumVal = Apple; 现在学到了一个比较优雅的用法:enumVal = Enum(constVal) 代码: #include <iostream> enum Fruits { Apple = 0, Banana = 1, Orang...原创 2018-09-05 18:04:37 · 6455 阅读 · 4 评论 -
浅拷贝与深度复制
#include <memory.h> struct sa { char a1[10]; int * p; sa() { memset(&a1, 0, sizeof(a1)); } }; int main() { sa a1; for (int i =0;i<10;++i) { a1.a1[i] = 'a'; } int intVal = ...原创 2018-08-14 19:57:18 · 116 阅读 · 0 评论 -
什么是回调函数
转载自知乎,很形象。你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货。在这个例子里,你的电话号码就叫回调函数,你把电话留给店员就叫登记回调函数,店里后来有货了叫做触发了回调关联的事件,店员给你打电话叫做调用回调函数,你到店里去取货叫做响应回调事件。作者:常溪玲链接:https://www.zhihu....转载 2018-02-23 17:17:47 · 175 阅读 · 0 评论 -
#define用法
转载自:https://www.cnblogs.com/Jency/articles/C_Cplusplus_define.html 好东西和大家一起学习下。以下内容有一些有一些有很浅显的拼写错误,但不影响阅读,连接字符ConCat("ABC","DEF")我在Qt上测试是编译不过的,不清楚VS下面可不可以。 正文内容: #define MAXTIME 1000 一个简单的转载 2017-12-15 11:03:23 · 546 阅读 · 0 评论 -
#define中#和##以及 #@的作用
转自http://blog.youkuaiyun.com/qq_15457239/article/details/56842450 与大家共同学习 代码 #include <stdio.h> #include <stdlib.h> #define f(a,b) a##b #define d(a) #a #define s(a) d(a) int main( void ) {...转载 2017-12-14 13:58:41 · 2592 阅读 · 0 评论 -
C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
好文章,当个搬运工。转自:http://blog.youkuaiyun.com/gmstart/article/details/7046140 在C++的类定义里面,可以看到类似下面的定义: 01 class List { 02 private: 03 Node *转载 2017-10-16 18:11:04 · 334 阅读 · 0 评论 -
简要总结良好的C++编程习惯
简要总结一些良好的编程习惯,一方面加深自己的印象,一方面与大家共勉。 1、使用指针前先判空。即: if(ptr) { ... } 2、删除指针前先判空,并赋空值。即: if(ptr) { delete ptr; ptr = NULL; //或者ptr = nullptr; } 3、遇到除数时先判零。即: ...原创 2017-10-24 18:52:09 · 447 阅读 · 0 评论 -
转载:C 指针传递变量为什么无法修改变量值
今天在知乎看好一份非常好的指针的陷阱,做个搬运工: 题目要求就是给以下函数纠错: void GetMemory( char*p ) { p = (char*) malloc( 100 ); } void Test( void ) { char*str = NULL; GetMemory(str); strcpy(str,"hello world" ...转载 2017-09-21 15:59:22 · 946 阅读 · 0 评论 -
指向指针的引用
听起来很复杂,其实按层次顺序理解就能容易,指向指针的引用:1、这是一个引用,2、这个引用引用的是一个指针。 牢记引用的实质:引用只是被引用对象/变量的别名。 测试环境:Qt, 示例代码: int a = 1; int b = 2; int * temp = &a; int *&p = temp; p = &b; *p = 5; qDebug()转载 2017-09-29 14:55:00 · 638 阅读 · 0 评论