some C++ questions
来源:互联网 发布:玻璃城堡gl知乎 编辑:程序博客网 时间:2024/05/10 20:53
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
- Some questions can test your C ability
- some questions
- Some questions
- Discovers some questions
- Some MySql Questions
- Some other questions
- Some questions about ConcurrentLinkedQueue
- some C++ questions
- Some Questions about eXtremeDB Log
- Some questions about RT-preempt
- Some questions about ambient noise
- Some interesting questions raised by Google
- Some questions on MapGuide Enterprise 2007
- Hydrogenaudio Forums Some questions from a
- Some common questions in English Interview
- Install caffe Some questions--in ubuntu 14.04
- Some Questions of Process API(1)
- some questions and tips about java
- git修改host的URL
- 简单的mvc类自动加载的编写与解析(二)
- 笔记:复杂网络的关键技术及应用
- android中设置ListView的选中的Item的背景颜色
- CVPR 2016-11-28
- some C++ questions
- 替黑马培训的同学插播个广告: 黑马程序员:前端2016年,满满60G的全套视频和案例无加密,我已经看了40%,想要的+qq:994630001
- 【Jquery】jquery点击按钮弹出div层
- 如何打造安全的容器云平台
- 记录springMvc + log4j
- 9.顺序容器
- 监听textfiled输入
- Linux中环境变量文件及配置
- tinny dnn错误解决办法