
C/C++
文章平均质量分 64
tmljs1988
这个作者很懒,什么都没留下…
展开
-
osg源码中的ref_ptr operator unspecified_bool_type() 指向类数据成员的指针&类转化为另一类型(如自定义的C类转化为int)
templateclass A{public: typedef T A::*unspecified_bool_type;//另一类型,指向T的指针,但该类型是属于A的,声明时要加A::,访问时应加a.. operator unspecified_bool_type() const { m_p= 3; return &A::m_p;//一定要用A:: }public: T m_p;};A a;A::unspecified_bool_type m= a;//或者A::unspecified_bool原创 2010-09-29 14:49:00 · 1277 阅读 · 0 评论 -
B继承自A,A指针无法隐式转换为B指针,函数参数只管指针类型,与实际指向对象无关
#include#include usingnamespace std; class A{}; class B:public A{}; void F( B& b){ cout"F( B& b)"}void F( A& a){ c原创 2012-12-09 21:16:31 · 12434 阅读 · 0 评论 -
数组名,数组首地址,a,&a,&a[0]
(1)指针数组: 是数组,但数组中的每个元素都是指针int *p[5];//如p[2]是指针,可*p[ 2]=3;(2)指向数组的指针: 是个指针,但它指向的是一个数组int a[5];int (*p)[5];//与前一行比较,*p相当于a,即p=&a;就像:int m;int *pm;//*pm就相当于m.pm=&m;p= &a;//可与前一行合并为int (*p)[5]=&原创 2011-08-20 18:58:34 · 15962 阅读 · 2 评论 -
C/C++网站
数据结构与算法 面向对象的C++设计模式 源代码 http://www.brpreiss.com/books/opus4/原创 2013-03-20 09:29:42 · 8564 阅读 · 0 评论 -
C++头文件相互引用,最好一个#include,另一个class C;
C++头文件相互#include时最好是:(1)在"CA.h"中 #include "CB.h". (2)在"CB.h"中用类的前向声明: class CA;(3)最好加上头文件卫士( #ifndef *** #define *** #endif)示例如下:(1)"CA.h":#ifndef HEADER_CA#define HEADER_CA原创 2010-12-16 22:51:00 · 23168 阅读 · 9 评论 -
二叉排序树的查找
二叉排序树的查找,如找不到某个值,则记录其父结点:Node* Search( Node* root, int key, Node* &prnt){ Node* p= root; while ( p != NULL) { if ( p->data == key) { return p; } prnt= p;//记录其父结转载 2013-04-03 19:58:14 · 9101 阅读 · 0 评论 -
B继承自A,A指针无法隐式转换为B指针,函数参数只管指针类型,与实际指向对象无关
#include #include using namespace std; class A{}; class B: public A{}; void F( B& b){ cout"F( B& b)"}void F( A& a){ cout"F( A& a)"<< endl; } int原创 2013-04-07 22:19:33 · 10298 阅读 · 0 评论 -
Node与NodeVisitor
osg::ref_ptr model;NodeVisitor vv( TRAVERSE_ALL_CHILDREN); model->accept( vv);假设model的模型结构如图:则model->accept( vv);多态,相当于调用Group::accept(NodeVisitor)。注意,我开始误认为Group没有对accept()进行重写,但后来在遍历原创 2013-04-07 22:47:02 · 16002 阅读 · 1 评论 -
智能指针(OSG源码摘录)
智能指针只是对指针添加引一个引用计数(_refCount).当new一个对象(即将对象赋予ref_ptr变量指向其它的对象或者将其释放(超出其生命范围,如局部变量)时,执行Referenced::unref(),_refCount会减1,此时如果_refCount为0表明此对象已经没有引用,可删除,delete this;会调用析构函数~Referenced(),并释放内存资源.原创 2010-07-27 15:58:00 · 1258 阅读 · 0 评论 -
多态时最好将基类的析构函数设为virtual
多态时最好将基类的析构函数设为virtual,这样在析构时会先调用子类的析构函数,再调用基类的析构函数,否则如果delete的是基类的指针,则只调用基类的析构函数.示例如下:#include class Base { public: Base() { mPtr = new int; } virtual ~Base() { delete原创 2012-11-04 20:01:10 · 14355 阅读 · 0 评论 -
虚函数、普通成员函数访问类的数据成员
虚函数、普通成员函数访问类的数据成员:class A { protected: int m_data; public: A(int data = 0){ m_data = data; } int GetData(){ return doGetData();} virtual int doGetData(){ return转载 2012-11-04 19:44:54 · 16046 阅读 · 0 评论 -
复杂指针定义
int (*(*F(int,int)))(int)是什么类型?首先,int(*M)(int),其中M为函数指针,则(*F)(int,int)=M,用N代替*F,F为指向N的指针,则N(int,int)=M,这表示N是一个函数,参数为2个int,其返回值为函数指针M。故F为函数指针,该函数指针所指向的函数的参数为2个int,返回值为函数指针。另:int a;int F();可以想原创 2012-11-07 15:13:42 · 12549 阅读 · 0 评论 -
多重继承 数据共享
class CFurniture{public: CFurniture(): m_iWeith( 3){ cout void SetWidth( int iWeight) { m_iWeith= iWeight; } void GetWidth( ) { cout }protected: int m_iWeith;};class CBed : virtual public CFurniture // {public: void SetWidth( int iWeight) { m_iWei转载 2010-09-29 14:53:00 · 814 阅读 · 0 评论 -
#ifndef 头文件卫士 只防止一个.cpp里的重定义(因为#define只作用于一个.cpp),而不是多个.cpp
#ifndef 头文件卫士是为了保证类的头文件在一个.cpp文件中被多次引用后会不会出现重复定义的问题,注意,只是防止在一个.cpp文件中被多次引用.#ifndef ELEMTYPE_H#define ELEMTYPE_H的意思是,如果前面没有定义ELEMTYPE_H,那么现在定义ELEMTYPE_H它不需要有值,只是表明是否被定义过,它是为了防止头文件的重复定义我举个简单的例子帮助你理解:你在a.h里定义了一个类a:class a{}然后在b.h里定义了一个类b,而且引用了a.h:#include "a.原创 2010-11-30 16:23:00 · 6970 阅读 · 0 评论 -
头文件卫士的作用
<br />1.两个文件:a.h, main.cpp. main.cpp中#include "a.h":<br />此方法最好不用,因为最好不要在头文件里定义变量:<br />(1)[a.h]:<br />int b;//只有一行,定义int变量<br />(2) [main.cpp]:<br />#include "a.h" //可以,但如果有其他.cpp文件引用a.h则重定义(多个.cpp中)出错. <br />main(){ ...}<br />----------------------------原创 2010-12-10 20:39:00 · 3877 阅读 · 0 评论 -
friend函数访问2个类的对象的私有成员
用friend可以让一个函数访问一个类的对象的私有成员,如果想让一个函数访问2个类的对象的私有成员,则必须在第2个类中声明第一个类为友元类,并且也声明该函数为友元函数"(1)CA.h:class CB;//前向声明使用CB类class CA{public: friend void FUNC( CA* pa, CB* pb);//友元函数}---------------------(2)CB.h:class CB{public: friend class CA;//声明CA为CB的友元类 friend voi原创 2010-12-16 21:13:00 · 2128 阅读 · 0 评论 -
在win32的窗口程序中加入控制台console窗口
在win32的窗口程序中加入控制台console窗口:(1)头文件:#include #include #include using namespace std;(2)redirectIOToConsole()函数://使用控制台的输出void redirectIOToConso原创 2011-07-14 10:45:22 · 2590 阅读 · 0 评论 -
HDACM1024 max sum plus plus:m个不相交子段的最大和
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1024HDACM1024 max sum plus plus:m个不相交子段的最大和:思路:dp[i][j]表示前j个元素分成i段的最优解,同时这个最优解是由a[j]元素结束的。转移方原创 2011-07-29 15:47:05 · 1400 阅读 · 0 评论 -
C++类的内存对齐
内存对齐。对下面的类: class B{ public:int m_a; short int m_b; double m_c private: int m_d;转载 2012-10-09 10:22:54 · 16094 阅读 · 0 评论 -
《程序员面试宝典》错误题:拷贝构造函数 浅拷贝、深拷贝
#include using namespace std; class CDemo{ public: CDemo():str(NULL){} ~CDemo(){if(str) delete [] str;} char *str; }; int main() { CDemo d1; d1.str = new char[32]; strcpy(d1.s转载 2012-11-07 11:59:52 · 14195 阅读 · 0 评论 -
类的sizeof大小
#pragma pack(n) 字节对齐用法详解对 齐的作用和原因:各个硬件平台对存储空间的处理上有很大的不同。一些平台对某些特定类型的数据只能从某些特定地址开始存取。其他平台可能没有这种情况,但 是最常见的是如果不按照适合其平台要求对数据存放进行对齐,会在存取效率上带来损失。比如有些平台每次读都是从偶地址开始,如果一个int型(假设为32 位系统)如果存放在偶地址开始的地方,那么一个读周期就转载 2012-11-07 17:01:00 · 13160 阅读 · 0 评论