c++ tips

来源:互联网 发布:3d人体模型软件 编辑:程序博客网 时间:2024/06/16 02:14
有const 防止赋值;只能通过拷贝构造函数

CMessage* const pMsg(PackFormatProgressIndic(m_guidthread, m_sessionIdthread, progress));

2 左值,右值

eConnStateT getConnState() 
{
return m_SipConnState;
}
   //错误:getConnState()没有return & ,所以不能做左值
  sipTerminal->getConnState() = kConnStateConnected;

一种是静态建立,如A a;另一种是动态建立,如A* p=new A(),A*p=(A*)malloc();静态建立一个类对象,是由编译器为对象在栈空间中分配内存


class IceSessionCallback
{
public:
    virtual void ReturnData(char * buf, int len, IceSession * session) = 0;
}

class ControlMainRe: public IceSessionCallback

{
    virtual void ReturnData(char* buf, int len, IceSession * session);

}

基类设置成了接口,子类一定要定义,因为得实现,而虚函数的子类同名函数一定是虚函数

5

char *p;
char *p="";
两者不一样,第一个p=0 ,没有分配内存;
第二个p分配了地址,内容是一个字符\0
6 string

main ()
{
        string str="abc";
        char *p1=(char*)str.c_str();

        cout <<p1<<endl;
        cout <<p1[0]<<endl;
        cout <<p1[1]<<endl;
        cout <<p1[2]<<endl;

        const char *p;

        p =(char*)malloc(sizeof(char)*str.length());
        p = str.c_str();

        cout <<"============="<<endl;
        cout <<p<<endl;
        cout <<"============="<<endl;

        int i=0;
        while (p)
        {
                cout <<(*p)<<endl;//因为没有结束符,所以print 越界
                p++;
                i++;

                if (i==20)
                        break;
        }
}

7 c++ 与c区别

除了main 函数文件,c++中class代替c中的函数,在class内部实现函数,给main调用,包了一层,因为包了一层,加了一些配套,构造函数等)

原创粉丝点击