易截截图软件、单文件、免安装、纯绿色、仅160KB

C的函数指针实现C++的多态

C的函数指针很强大,用好了才是C语言的高手。像Gtk中的回调函数的使用,都体现了函数指针的强大威力。
struct Point{
int x, y;
};
/*Shape*/
/*----------------------------------------------------------------*/
struct Shape {
struct Methods* methods;
};

struct Methods {//将C++对应类中所有成员函数封装到一个结构体里面
float (*getCircle)(Shape * shape);
float (*getArea)(Shape * shape);
void (*draw)(Shape * shape);
void (*move)(Shape * shape);
};
////////////////C++ Counterpart of above code//////////////////
class Shape {
public:
virtual float getCircle() = 0;
virtual float getArea() = 0;
virtual void draw() = 0;
virtual void move() = 0;
};
///////////////////////////////////////////////////////////////
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/*Rectangle*/
struct Rectangle {
struct Methods * methods; //包含封装后的成员函数结构体的指针
Point leftTop; //Shape之外派生的成员变量
Point rightBottom; //Shape之外派生的成员变量
};
float Rectangle_getCircle(Shape * shape)
{
Rectangle * r = (Rectangle *) shape;
return (r->rightBottom.x - r->leftTop.x +
r->rightBottom.y - r->leftTop.y) * 2;
}
float Rectangle_getArea(Shape * shape){}
void Rectangle_draw (Shape * shape){}
void Rectangle_move(Shape * shape){}
struct Methods rectangleMethods = {
&Rectangle_getCircle,
&Rectangle_getArea,
&Rectangle_draw,
&Rectangle_move,
};
////////////////C++ Counterpart of above code//////////////////
class Rectangle : public Shape {
Point leftTop;
Point rightBottom;
public:
virtual float getCircle();
virtual float


相关文档:

C/C++运算符的优先级


Precedence Operator Description Example Overloadable Associativity
1
::
Scope resolution operator
Class::age = 2;
no
none
2
()
()
[]
->
.
++
--
const_cast
dynamic_cast
static_cast
reinterpret_cast
typeid
Function call
Member initalization
Array access
Member access from ......

C/C++试题

1、bool、float、指针变量与"零值"比较的if语句?
答:
bool flag;   if(flag),if(!flag)
char *p;   if(p==NULL),if(p!=NULL)
float x;  
const float EPSILON = 1e-6;
if((x>=-EPSILON)&&(x<=EPSILON)) //(-0.000001~0.000001)
if((x<-EPSILON)&& ......

c/c++程序的内存分配 [转]

题记:
所有的完美,都是在崩溃的一刻达到的!
我一直回避程序的内存管理,因为爱之愈深,恨之愈烈。但是,还是由很多的朋友一直在体这方面的问题,所以就索性把它坦白了,也许对你我都是一件好事情。
首先,需要搞清楚:变量的类型和它的存储类别是两个概念。
数据类型和内存管理没有直接的关系。
一个由c/C++编 ......

Ubuntu中NetBeans C/C++配置、编译

系统环境:Ubuntu 9.04
软件环境:NetBeans 6.7.1 C/C++ 、JDK1.6.0_16
本次目的:完成NetBeans 6.7.1 C/C++ 的配置工作、编译测试及对中文支持
      首先从官网上下载最新版的Netbeans 选择C/C++工作台下载[点击进入],弹出的新网页将会自动下载,如下图:
在进行安装之前,我们先安装JDK, ......

C字符串长度定义规范

C字符串长度定义规范
(转载请注明来源于金庆的专栏)
因为总是见到C字符串长度相关的错误, 所有定个规范出来, 有利于减少错误.
关键在于C字符串有个结尾0, 处理时需要注意.
定义字符串长度有两种方式, 一种是用字符串的大小, 一种是字符串的长度.
字符串的大小即内存空间的大小, 而串长度加1才是所需空间.   ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号