Impossible is nothing  
  爱过知情重醉过知酒浓   花开花谢终是空   缘份不停留像春风来又走   女人如花花似梦
公告
日历
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 8
  • 文章 - 91
  • 评论 - 16
  • 引用 - 0

导航

常用链接

留言簿(4)

随笔分类(4)

随笔档案(8)

文章分类(77)

文章档案(91)

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

1. c++中类的相互引用
 
  原则是:相互引用的class要分别写.h和.cpp文件(分别合用一个.h,.cpp也可)
         在.h文件中只需申明class类型即可,一定不要包含其他类的头文件
         在.cpp文件中必须要包含其他要引用的头件
         不要将函数申明跟寒暑提在同一文件中实现,否则会出意想不到的错误!!!
        
         a.h b.h 合成一个.h文件
         a.cpp b.cpp 合成一个.cpp文件也可
        
  a.h 
  #ifndef _A_
  #define _A_
  
  class b;
  class a;
  
  class a {
   friend class  b;
  private:
   int aa;  
   void a1( b m );
  };
  #endif
   a.cpp
        #include "stdafx.h"
  #include "a.h"
  #include "b.h"
  
  void a::a1(b m )
  { 
   m.bb = 0 ;
  }
   b.h
  #ifndef _B_
  #define _B_
  class  b;
  class a;
  
  class  b
  {
   friend class a;
  private:
   int bb;
   void zzz(a n);
  }; 
  #endif
 b.cpp
     #include "stdafx.h"
  #include "b.h"
  #include "a.h"
  void b::zzz(a m )
  { 
   m.aa = 0 ;
  } 
 
   main.cpp
  #include "stdafx.h"
  #include "a.h"
  #include "b.h"
  
  int main(int argc, char* argv[])
  {
  
      a aa;
      b bb;
  
      return 0;
  }

2. 链表的好用法
   struct a {
     static a *mLinkedList; // 申明为一个静态变量
 
     a *mNext;
     bool mCanRemoteCreate;

    a(bool canRemoteCreate)
    {
       mNext = mLinkedList;
       mLinkedList = this;
       mCanRemoteCreate = canRemoteCreate;
    }
    static int *create(const char *name);
  };
 
  a *a::mLinkedList = NULL; // 初始化
 
3. 灵活的应用# ##
Token-Pasting Operator (##)

#define paster( n ) printf( "token" #n " = %d", token##n )
int token9 = 9;
If a macro is called with a numeric argument like
paster( 9 );the macro yields
printf( "token" "9" " = %d", token9 );which becomes
printf( "token9 = %d", token9 );

Stringizing Operator (#)
#define stringer( x ) printf( #x "\n" )
void main()
{
    stringer( In quotes in the printf function call\n );
    stringer( "In quotes when printed to the screen"\n );  
    stringer( "This: \"  prints an escaped double quote" );
}
Such invocations would be expanded during preprocessing, producing the following code:
void main()
{
   printf( "In quotes in the printf function call\n" "\n" );
   printf( "\"In quotes when printed to the screen\"\n" "\n" );
   printf( "\"This:
\\\" prints an escaped double quote\"" "\n" );
}
When the program is run, screen output for each line is as follows:In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quotation mark"
#define IMPLEMENT_NETCONNECTION(className, classGroup, canRemoteCreate) \
   NetClassRep* className::getClassRep() const { return &className::dynClassRep; } \
   NetClassRepInstance<className> className::dynClassRep(#className, 0, NetClassTypeNone, 0); \
   NetClassGroup className::getNetClassGroup() const { return classGroup; } \
   static NetConnectionRep g##className##Rep(&className::dynClassRep, canRemoteCreate)

4. 枚举:初始化为0值开始,后者比前者大1,除非显式指定.
   By default, the first enumerator has a value of 0, and each successive enumerator is one larger
   than the value of the previous one, unless you explicitly specify a value for a particular
   enumerator. Enumerators needn’t have unique values. The name of each enumerator is treated
   as a constant and must be unique within the scope where the enum is defined. An enumerator
   can be promoted to an integer value. However, converting an integer to an enumerator requires
   an explicit cast, and the results are not defined.

=========================
一些优秀的数学算法
5.1 /// Determines if number is a power of two.
 inline bool isPow2(const U32 number)
 {
    return (number & (number - 1)) == 0;
 }
5.2 浮点数的计算机中的储存方法

    单精度      1|   8   |   23    |
             符号  指数      尾数
    双精度      1|   11  |   52    |
             符号  指数      尾数  
            
    10110.100011 -> 1.0110100011* 2(4) 2的4之方
   
    符号位 0
    尾数   0110100011
    指数   4 以过剩127储存 +127= 131  -> 10000011
    所以  IEEE 754 : 0100000110110100011
   
    -0.0010011  -> -1.0011 * 2(-3) 2的-3之方
    符号位:-1
    尾数  : 0011
    指数为:-3  +127  的124 -〉01111100
    所以: 1 01111100 0011000000000000000000
   
    /// Determines the binary logarithm of the input value rounded down to the nearest power of 2.
 inline U32 getBinLog2(U32 value)
 {
    F32 floatValue = F32(value);
    return (*((U32 *) &floatValue) >> 23) - 127;
 }

posted on 2006-03-03 15:30 笑笑生 阅读(243) 评论(0)  编辑 收藏 引用 所属分类: C++语言

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © 笑笑生 Powered by: 博客园 模板提供:沪江博客