![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
编程语言 c++
勇zhe无wei
这个作者很懒,什么都没留下…
展开
-
关于c++中类的继承与派生
#includeusing namespace std;class A{public: int x; A(int a,int b){ a=x; b=u;} void prt(){ cout<<cout<<x<<endl;}private: int u;};class B:public A{public: B原创 2012-11-13 11:00:20 · 829 阅读 · 0 评论 -
程序内存泄露
内存泄露的含义是程序在运行时动态的分配内存空间,但是用完之后并未释放,随着时间的延长,内存会被渐渐地消耗,知道被耗尽,直接导致整个系统崩溃。这里的内存并非是物理上的,而是虚存的大小。内存泄露是从操作系统的角度来说的。内存泄露并不是物理上内存的消失,而是失去对内存空间的访问,造成内存的浪费!导致内存泄露的原因有很多:1.程序员在使用 malloc 或 new 申请内原创 2013-12-25 15:47:40 · 2469 阅读 · 0 评论 -
c++文件操作std::ifstream
C++对文件操作相关:ifs.is_open() 判断文件是否打开ifs.get() 获取文件的一个字符ifs.good() 判断文件是否结束std::ifstream::in 以只读的方式打开#include #include // std::ifstreamusing namespace std;int main原创 2015-02-04 20:51:54 · 38846 阅读 · 3 评论 -
smart pointer (unique_ptr)
smart pointer:unique_ptr只能有一个指针指向某块内存区域。1.不支持赋值和复制构造unique_ptr ap(new int(88));unique_ptr aa(ap);出错unique_ptr bb(aa));出错2.可以移动构造和移动赋值在函数中作为返回值可以用unique_ptr Getval{ unique_ptr原创 2015-02-05 10:28:01 · 1263 阅读 · 0 评论 -
C++ 11 std::function std::bind使用
std::function是C++11的新特性——可调用对象模板类。std::function代表一个可调用对象,接收0个参数,返回void。std::function可以绑定到全局函数和静态函数,如果要绑定到类的非静态成员函数,则需要使用std::bind。#include #include using namespace std;typedef std::functio原创 2015-02-04 19:19:56 · 2321 阅读 · 0 评论 -
std::bind
int f(int, char, double);auto frev = bind(f, _3, _2, _1); // 翻转参数顺序int x = frev(1.2, ‘c’, 7); // f(7, ‘c’, 1.2);此处,auto关键字节约了我们去推断bind返回的结果类型的工作。bind不仅可以绑定函数,也可以绑定变量原创 2015-02-04 20:17:09 · 664 阅读 · 0 评论 -
localtime和localtime_r
在工作中 突然用到时间函数,一查才发现localtime和localtime_r原来差别这么大。1. loacltime 不可重入,localtime_r可重入。2. localtime使用两次,前面的值会被重写。 #include #include int main(int argc,char *argv){ char snow[50]={0},send[50]={0}原创 2015-02-05 09:36:56 · 1813 阅读 · 0 评论 -
dlopen,dlsym,dlclose的使用
dlopen dlsym dlclose的应用在写程序时,我们经常使用动态库,其实,上面提供的三个函数也可以调用动态库。- 创建动态库#include void test(){ printf("test hello!\n");}command : # gcc -shared -o test.so test.c -fPIC-callback#原创 2015-02-05 13:53:41 · 925 阅读 · 0 评论 -
设计模式--代理模式
代理模式在我们的生活中代理的例子是非常多,对我们IT人员最直接的就是上Google,由于GFW屏蔽了Google,因此我们需要通过代理访问Google。 还有我们买火车票,有些人买不到,通过黄牛作为一个中介买车票。黄牛就是一个代理。设计模式中定义: 为其他对象提供一种代理以控制对这个对象的访问. #include <iostream>using namespace std;class gir原创 2015-03-05 17:04:34 · 563 阅读 · 0 评论 -
设计模式--外观模式
外观模式外观模式是一个子系统的封装,通过访问外观来实现访问子系统。只要定义好了这个接口,来创造出新的子系统是非常容易。#include <iostream>using namespace std;class fact_one{public: void sell(){ cout<<"sell fact_one"<<endl; } void buy(原创 2015-03-05 15:33:29 · 435 阅读 · 0 评论 -
设计模式--适配器模式
适配器模式*代码图片 from:bkeey#include <iostream>using namespace std;class deque{public: void push_back(int x){ cout<<"deque push_back"<<endl; } void push_front(int x){ cout原创 2015-03-06 10:59:18 · 562 阅读 · 0 评论 -
C++ 智能指针
由于C++ 中没有自动内存回收机制,每当程序员在堆上new 一块内存时都要通过delete释放掉。如果程序员忘记释放,可能会异常导致内存泄露,程序过早退出。如果在程序中应用智能指针,可以有效缓解这类问题。智能指针的类型主要有boost::smart_ptrscoped_ptrscoped_arrayshared_ptrshared_array智能指针原创 2013-12-23 18:55:18 · 836 阅读 · 0 评论 -
c++中多继承同名隐藏的两种继承方式
虚基类的作用是为了避免派生类从基类中继承过来的同名成员同时产生多个副本。图解:祖父类->两个父亲类-》一个孩子类#includeusing namespace std;class base0{ public: int var0; void fun0(){cout<<"member of baseo"<<endl;}};class base1:virtual public base原创 2012-11-15 07:27:56 · 1875 阅读 · 3 评论 -
常量指针 指针常量
int a=1;常量指针: int const *p=&a;常量指针可以理解为指向常量的指针,不能通过指针来改变常量的值; E: *p=5; 错误 ,并且p指向的地址可以改变int b=5;指针常量:int *const p=&b;指针常量一旦指向某个地址空间,它的地址将永远不会改变,也可以用它来改变空间存储的值 *p=10 ;正确#includeusing原创 2013-12-23 23:44:57 · 941 阅读 · 0 评论 -
c++中的类型兼容规则(基类对象)
#includeusing namespace std;class Base1{public: void diaplay() const{ cout<<"enter Base1::display"<<endl; }};class Base2:public Base1{public: void diaplay() const{ cout<<"enter B原创 2012-11-13 17:04:34 · 2061 阅读 · 0 评论 -
c++中私有继承和公有继承的特点
当类的继承方式为私有继承时,基类中的公有成员和保护成员都以私有成员的身份出现在派生类中,而基类中的私有成员在派生类中不可直接访问。上面继承类对象调用的函数都是派生类自身的公有成员,因为私有继承不可访问基类中的成员#include using namespace std;class point {public:void xpoint(int x=0,int y=0){this原创 2012-11-13 18:01:16 · 1598 阅读 · 0 评论 -
c++枚举类型
枚举类型用关键词enum定义,枚举类型简单的来说就是列举 enum box{pencil,pen};//这里你就定义了一个枚举类型的变量叫box,这个枚举变量内含有两个元素也称枚举元素在这里是pencil和pen,分别表示铅笔和钢笔。 enum box{pencil,pen}; enum box box2;//或者简写成box box2;原创 2012-12-12 22:37:55 · 849 阅读 · 0 评论 -
C++遇到问题了,编译正确,但运行不行啊
#includeusing namespace std;class myCla{int arr[10];public:void myIn();void myOut();};void myCla::myIn(){cout>arr[i];}void myCla::myOut(){cout<<"-----The resule -----"<=0;i--)cout<<arr[i]<<" ";cout<<e原创 2012-10-08 22:53:40 · 2150 阅读 · 8 评论 -
c++中静态和动态生存期对象的时钟程序运行问题
程序运行不了了!!!! 高手看看吧!#includeusing namespace std;class clock{ //时钟类定义 public: //外部接口 clock(); //构造函数 void settime(int newH,int newM,int newS); void showtime(); :clock():原创 2012-10-21 22:58:37 · 1964 阅读 · 1 评论 -
虚基类及其派生构造函数与析构函数u
构造一个类的对象的一般顺序是:(1):如果有直接或间接的虚基类,先执行虚基类的构造函数(2):如果尤其它基类,则按照他们在继承声明列表中的出现次序,分别执行他们的构造函数,但构造过程中,不再执行虚基类的构造函数。#includeusing namespace std;class base0{ public: base0(int var):var0(var){cout<<"ggg原创 2012-11-16 15:06:59 · 1456 阅读 · 0 评论 -
c++使用联合体保存成绩,并且输出
#include#includeusing namespace std;class Examinfo{ public: Examinfo(string name,char grade):name(name),mode(GRADE),grade(grade){} Examinfo(string name,bool pass):name(name),mode(PASS),pass(pass原创 2012-10-21 17:32:43 · 1872 阅读 · 3 评论 -
c++模板 (函数模板和类模板)
模板的作用时实现代码的重用,它可以将数据类型定义为参数,模板分为函数模板和类模板。函数模板: template 返回类型 函数名(形参表){//函数体} #includeusing namespace std;templateT min(T a, T b){ return a<b?a:b; }int main(){ int a=2,b=10;原创 2013-12-30 11:25:59 · 958 阅读 · 0 评论 -
最简单的c++编程器测试程序
#includeusing namespace std;int main(){}原创 2012-10-21 11:50:48 · 1347 阅读 · 0 评论 -
内联函数
内联函数是c++中一个非常使用的函数机制,相当于c语言中的宏,但是内联函数与宏又有不同之处.内联函数是定义在类体中的成员函数,引入内联函数的目的是 解决程序中函数调用的效率问题,函数的代码会被放到符号表中,使用时直接替换,像宏一样展开。内联函数有的使用减少了函数调用的开销,效率得到提高。内联函数是通过编译器来实现替换的,而宏则是在预编译的时候替换。禁止内联函数的方法: #p原创 2013-12-25 16:11:41 · 1039 阅读 · 0 评论 -
C++ 编译出现字符串常量转化问题
出现问题原因:char *msg[26] = { "","","GET","POST","", "","OTHER","","","", "","","","","", "","","","","", "","HEAD","PUT","DEL原创 2015-10-06 11:59:16 · 3402 阅读 · 0 评论