![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
liuxizhiyi
next time
展开
-
Inside the C++ Object Model chapter 1
1.1.1 简单对象模型 对象由一系列的slots组成,每个slot中存放着指向对象的一个成员(数据成员与函数成员)的指针,成员按其在类中声明的顺序排序,查找成员等效于查找其在对象中的索引。类对象大小的计算方式:指针大小乘以成员数目(参见书-图1.1)。 本模型采用空间与执行期效率为代价简化编译器设计复杂度,对象的大小不会因为成员类型的不同而改变――类对象中存储的都是指针。 本模型关于原创 2008-10-23 12:40:00 · 528 阅读 · 0 评论 -
C++禁止继承
Analyzing the problemThe problem is very simple: create a mechanism whereby its possibleto mark a class (say, NotABase) as being uninheritable, such thatattempting to derive a class from转载 2008-11-01 13:24:00 · 1667 阅读 · 0 评论 -
Bit Twiddling Hacks
Bit Twiddling Hacks By Sean Eron Andersonseander@cs.stanford.edu Individually, the code snippets here are in the public domain (unless otherwise noted) — feel free to use them however you please. Th转载 2008-10-23 19:33:00 · 984 阅读 · 0 评论 -
Smart Pointer
#include using namespace std;class Object{ string name,value;public: void getName(){cout void getValue(){cout};class Container{ vector vec;public: void add(Object* obj){原创 2008-10-23 12:59:00 · 381 阅读 · 0 评论 -
只能用一条语句输出
写一个函数 int p(int i, int N); 能够输出i到N再到i,即以参数1,7调用函数,输出结果为 1 2 3 4 5 6 7 6 5 4 3 2 1 要求只用一个语句完成,不允许用?:等n多操作符和关键字。只能用一个printf库函数int p(int i,int N){ return (printf(原创 2008-10-24 14:19:00 · 573 阅读 · 0 评论 -
pointer to member
class widget{public: int a; void h(int); void g(int);};void widget::g(int i){}main(){ widget w; int widget::* pm=&widget::a; void (widget::*pmem)(in原创 2008-10-23 13:03:00 · 426 阅读 · 0 评论 -
Function Pointer
void * (*(*fp1)(int))[10];fp1是一个指向函数的指针,该函数接受一个int参数并返回一个指向含有10个void指针数组的指针float (*(*fp2)(int,int,float))(int);fp2是一个指向函数的指针,该函数接受三个参数(int,int,float)且返回一个指向函数的指针,该函数接受一个int参数并返回floattypedef do原创 2008-10-23 13:01:00 · 401 阅读 · 0 评论 -
template
#pragma once #include #include #include //allocator #include using namespace std;templateclass T,size_t N=10>class Array{ T data[N]; size_t count;public: Array(){c原创 2008-10-23 12:50:00 · 466 阅读 · 0 评论 -
Stream buf
#include #include int main(int argc, char* argv[]){ assert(argc==2); ifstream in(argv[1]); assert(in); cout}...main(){ ifstream in("aa.cpp");原创 2008-10-23 13:04:00 · 112 阅读 · 0 评论 -
two types of template
templateclass T,int len>void QuickSort(T (&list)[len],int left,int right){ if(left>=right) return; int mid=RandomPartition(list,left,right); QuickSort(list,left,mid-1); QuickSort(l原创 2008-10-23 13:02:00 · 373 阅读 · 0 评论 -
operator->*
#include using namespace std;class Dog{public: int run(int i)const{ cout"run" return i; } int eat(int i)const{ cout"eat" return i; } int sleep(int i)const{ cout"sle原创 2008-10-23 12:57:00 · 780 阅读 · 0 评论 -
C++ unexpected throw exception
#pragma once #include class Up{};class Fit{};void g();void f(int i)throw(Up,Fit){ switch(i){ case 1: throw Up(); case 2: throw Fit(); } g();}void g(){ in原创 2008-10-23 12:54:00 · 716 阅读 · 0 评论 -
Container
你介意如果容器的底层使用了引用计数吗?如果是,你就得避开string,因为很多string的实现是用引用计数(参见条款13)。你也不能用rope,因为权威的rope实现是基于引用计数的(参见条款50)。于是你得重新审核你的string,你可以考虑使用vector。 你需要插入和删除的事务性语义吗?也就是说,你需要有可靠地回退插入和删除的能力吗?如果是,你就需要使用基于节点的容器。如果你需要多元原创 2008-10-23 12:49:00 · 989 阅读 · 0 评论 -
mem_fun/mem_fun_ref
template // 用于不带参数的non-const成员函数 mem_fun_t // 的mem_fun声明。 mem_fun(R(C::*pmf)()); // C是类,R是被指向 list lpw; // 同上 ... for_each(lpw.begin(), lpw.end(), mem_fun(&Widget::test)); // 这个现在可以编译了原创 2008-10-23 12:48:00 · 446 阅读 · 0 评论 -
friend template
templateclass T>class Friendly;templateclass T>void f(const Friendly&);templateclass T> class Friendly{ T t;public: Friendly(const T& theT):t(theT){} friend void fconst原创 2008-10-23 12:47:00 · 549 阅读 · 0 评论 -
curiously recurring template pattern
#include using namespace std;templateclass T> class Counted{ static int count; public: Counted(){++count;} Counted(const Conted&){++count;} ~Counted(){--count;} st原创 2008-10-23 12:46:00 · 454 阅读 · 0 评论 -
template metaprogram
#include using namespace std;templateint n>struct Factorial{ enum{val=Factorial::val*n};};templatestruct Factorial{ enum{val=1};};templateint N,int P> struct Power{ enum{val=N原创 2008-10-23 12:44:00 · 510 阅读 · 0 评论 -
Read-only data members
Read-only data membersOne means of achieving this (proxies are another, properties are a third)is by providing a public data member which is a const referenceto an actual (non-const) priv原创 2008-11-01 13:34:00 · 593 阅读 · 0 评论