C: 面向对象(3)
以下代码演示如何用C来模拟多态。gcc版本:3.4.4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#ifndef class
#define class struct
#endif
#ifndef private
#define private
#endif
#ifndef public
#define public
#endif
#ifndef protected
#define protected
#endif
#ifndef bool
#define bool int
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
class Parent{
//private members
private class Parent *this;
private size_t len;
//public members
public size_t (*length)(class Parent *this);
public void (*construct)(class Parent *this);
public void (*print)(class Parent *this);
public void (*destruct)(class Parent *this);
};
class Son{
//private members
private class Son *this;
//inherit
private class Parent *inherit;
//public members
public size_t (*length)(class Son *this);
public bool (*construct)(class Son *this);
public void (*print)(class Son *this);
public void (*destruct)(class Son *this);
};
//forward declaration
void ParentConstruct(class Parent *this);
void ParentPr
相关文档:
C的变参问题与print函数的实现
我们在C语言编程中会遇到一些参数个数可变的函数,例如printf() 这个函数,它的定义是这样的: int printf( const char* format, ...);
它除了有一个参数format固定以外,后面跟的参数的个数和类型是可变的,例如我们可以有以下不同的调用方法:
printf("%d",i);
&nb ......
static
C++中的static
C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static。前者应用于普通变量和函数,不涉及类;后者主要说明static在类中的作用。
一、面向过程设计中的static
1、静态全局变量
在全局变量前,加上关键字static,该变量就被定义成为一个静态全局变 ......
一个正则表达式的教程可以参看(里面有个测试正则表达式的工具)
http://unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm#ad
正则表达是用来匹配字符串的好东东。
如果用户熟悉Lin ......
Linux中的进程通信
1.管道
函数原型:int pipe(int filedes[2]);
函数返回值: 正确返回0;错误返回-1
其中的文件描述符filedes[0]是用来读取数据的,filedes[1]是用来写数据的。
例子1:
#include<stdio.h>
#include<stdlib.h>
#include<u ......
(一)整数符号的陷阱
(二)浮点数的本质
(三)堆栈的内存管理结构
(四)符号解析
(五)对齐和总线错误
(六)函数指针
(七)虚函数的实现机理
(八)引用的实现机理
(九)虚拟继承对象的内存结构
(十)混合编程时的初始化顺序
(十一)数组和指针的异同 ......