some C++ questions

PS:Using this  diary to record today's studying.Forgive me use English,because there is no chinese input method this computer,and for coryright,I have no permission to install any software.ha ha.2014-6-23

1.The difference const char*, char const* and char*const
  char a ='a';
  const char * p =&a;
  *p ='b';//Illegal,in this kind,p is a point point to const char(a),can not use p to change a's vaule.
  
  char a ='a';
  char const *p =&a;
  *p ='b';//this is the same kind of above.
  
  
  char a ='a';
  char * const p =&a;
  *p ='b';//legal,in this kind,p is a const point,can not change p's value,but we can use p to change a's value. 
   p=NULL;//Illegal.
2.Is Null Point can access class member funtion?
  class A
  {
  public:
   A()
   {
     m_a =0; 
 }
   virtual ~A()
   {
      m_a =-1;
   }
   void t1()
   {
    printf("t1\n");
   }
   void t2()
   {
     printf("%d",m_a);
   }
   void t3()
   {
   printf("%d",m_b);
   }
   private:
   static int m_b;
   int m_a;
  };
 int A::m_b=0;
 A *a =NULL;
 a->t1();//succ
//the function t1's address can be sure when complie,Assumption it's 0X00000010,the call a->t1() can be convert to (*0X00000010)(); funny?
 a->t2();//fail
 a->t3();//succ
 a->~A();//fail

 Amazing?Usually,we think that we can not use a NULL point to access a obj's funciton.But in factually,there are some codes can be executing corrcet above.
 You can find the rule above codes,it will success if a function not access the class no-static member variable.but if we access member variable and not static,it will failed.
 As class's member function,all objects instance of this class share the function code,the funcion address can be sure when compile,so even a NULL point,it can access the function codes.
 when a obj call member function,a hidden parameter this point[obj point] will be delivery to the function by C++ rules.all the member function or member variable in this function will be change to access via this point.like this:
 
void A::t2(A *this)
{
   printf("%d",this->m_a);//in this kind,if this point is NULL,will happen NULL Point access,it's Access violation.
}
a->t2(a); 
    And a static member variable is belong the class,it's address can be sure when complie,so,ever a NULL object point can access it.

3. Callback funtion.
   In network,system,or other field,Event-Driven programming is very popular.so we sometimes register a funtion point to the the module,it will call our function when some event happened. 
   class Login
   {
    static void LoginCallBack(message * msg,void *caller)
 {
  if(!msg||!caller)return;
  Login * i_login =(Login *) caller;//use this point do something.
  i_login->req_enter_game();
  //req_enter_game();//invaild,in this static function scope, it can not access a no static function.
 }
 void req_enter_game()
 {
   ....;
 }
    void doLogin()
 {
   message msg;//send msg to server,and call LoginCallBack functon  when the server response .
   msg.setcallback(&Login::LoginCallBack,this);
   sendMsg2Srv(&msg);
 }
   };
   You can find that the funciton LoginCallBack must be static,if not,it can not to convert to a function point.But the question is if the LoginCallBack is static,we know that a static funcion can not access no-static member method or variable,so,it will limit our develop. 
   We can do what?in fact,we deliver a static address to moudle we also deliver the obj this point to the module,when the module call back our funcion,this point will be deliver to the funtion.so we can convert the point to object point.after,we can use this object point do anything.

4. C++ template.
   Strangely,when our use C++ template,we should put template define and Declaration in a file if not,it's can not compile success.Like this:
   UserPorfile.h
   class UserPorfile
   {
     public:
  UserPorfile();
  virtual ~UserPorfile(){}
  template<typename T>
  void SetVaule(const char * szKey, T & value);
     private:
     Json::Value m_profile;
   };
   
   UserPorfile.cpp
   #include "UserPorfile.h"
   template<typename T>
  void UserPorfile::SetVaule(const char * szKey, T & value)
  {
    if(!szKey)return;
    if(!strlen(szKey))return;
    m_profile[szKey]=value;
  }
   It's can not compile success above code.I ask some industry elites for Solution,and I see some large Porjects's Codes they will put the template define to a special file,usally named ***_T.cpp or ***_T.h and include this file is template Declaration head file like below:
   
   UserPorfile.h
   class UserPorfile
   {
     public:
  UserPorfile();
  virtual ~UserPorfile(){}
  template<typename T>
  void SetVaule(const char * szKey, T & value);
     private:
     Json::Value m_profile;
   };
   #include "UserPorfile_T.cpp"
  
  UserPorfile_T.cpp
  #ifndef __USERPROFILE_T_CPP__ //must define this macro otherwise will trigger circle include.
  #define __USERPROFILE_T_CPP__
  #include "UserPorfile.h"
   template<typename T>
  void UserPorfile::SetVaule(const char * szKey, T & value)
  {
    if(!szKey)return;
    if(!strlen(szKey))return;
    m_profile[szKey]=value;
  }
   #endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Visual C++ 常用数值算法集 作者:何光渝编 出版社:科学出版社 出版日期:2002年7月 ISBN:703010498 序 前言 第1章 线性代数方程组的解法 1.1全主元高斯-约当(Gauss-Jordan)消去法 1.2LU分解法 1.3追赶法 1.4五对角线性方程组解法 1.5线性方程组解的迭代改善 1.6范德蒙(Vandermonde)方程组解法 1.7托伯利兹(Toeplitz)方程组解法 1.8奇异值分解 1.9线性方程组的共轭梯度法 1.1对称方程组的乔列斯基(Cholesky)分解法 1.11矩阵的QR分解 1.12松弛迭代法 第2章 插值 2.1拉格朗日插值 2.2有理函数插值 2.3三次样条插值 2.4有序表的检索法 2.5插值多项式 2.6二元拉格朗日插值 2.7双三次样条插值 第3章 数值积分 3.1梯形求积法 3.2辛普森(Simpson)求积法 3.3龙贝格(Romberg)求积法 3.4反常积分 3.5高斯(Gauss)求积法 3.6三重积分 第4章 特殊函数 4.1Γ函数、贝塔函数、阶乘及二项式系数 4.2不完全Γ函数、误差函数 4.3不完全贝塔函数 4.4零阶、一阶和任意整数阶的第一、二类贝塞尔函数 4.5零阶、一阶和任意整数阶的第一、二类变形贝塞尔函数 4.6分数阶第一类贝塞尔函数和变形贝塞尔函数 4.7指数积分和定指数积分 4.8连带勒让德函数 附录 第5章 函数逼近 5.1级数求和 5.2多项式和有理函数 5.3切比雪夫逼近 5.4积分和导数的切比雪夫逼近 5.5用切比雪夫逼近求函数的多项式逼近 第6章 随机数 6.1均匀分布随机数 6.2变换方法——指数分布和正态分布随机数 6.3舍选法——Γ分布、泊松分布和二项式分布随机数 6.4随机位的产生 6.5蒙特卡罗积分法 第7章 排序 7.1直接插入法和Shell方法 7.2堆排序 7.3索引表和等级表 7.4快速排序 7.5等价类的确定 附录 第8章 特征值问题 8.1对称矩阵的雅可比变换 8.2变实对称矩阵为三对角对称矩阵 8.3三对角矩阵的特征值和特征向量 8.4变一般矩阵为赫申伯格矩阵 8.5实赫申伯格矩阵的QR算法 第9章 数据拟合 9.1直线拟合 9.2线性最小二乘法 9.3非线性最小二乘法 9.4绝对值偏差最小的直线拟合 第1章 方程求根和非线性方程组的解法 1.1图解法 1.2逐步扫描法和二分法 1.3割线法和试位法 1.4布伦特(Brent)方法 1.5牛顿-拉斐森(Newton-Raphson)法 1.6求复系数多项式根的拉盖尔(Laguerre)方法 1.7求实系数多项式根的贝尔斯托(Bairstou)方法 1.8非线性方程组的牛顿-拉斐森方法 第11章 函数的极值和最优化 11.1黄金分割搜索法 11.2不用导数的布伦特(Brent)法 11.3用导数的布伦特(Brent)法 11.4多元函数的下山单纯形法 11.5多元函数的包维尔(Powell)法 11.6多元函数的共轭梯度法 11.7多元函数的变尺度法 11.8线性规划的单纯形法 第12章 傅里叶变换谱方法 12.1复数据快速傅里叶变换算法 12.2实数据快速傅里叶变换算法(一) 12.3实数据快速傅里叶变换算法(二) 12.4快速正弦变换和余弦变换 12.5卷积和逆卷积的快速算法 12.6离散相关和自相关的快速算法 12.7多维快速傅里叶变换算法 第13章 数据的统计描述 13.1分布的矩——均值、平均差、标准差、方差、斜差和峰态 13.2中位数的搜索 13.3均值与方差的显著性检验 13.4分布拟合的X2检验 13.5分布拟合的K-S检验法 第14章 解常微分方程组 14.1定步长四阶龙格-库塔(Runge-Kutta)法 14.2自适应变步长的龙格-库塔法 14.3改进的中点法 14.4外推法 第15章 两点边值问题的解法 15.1打靶法(一) 15.2打靶法(二) 15.3松弛法 第16章 偏微分方程的解法 16.1解边值问题的松弛法 16.2交替方向隐式方法(ADI) 参考文献 编后记

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值