const:

  1 定义变量 ,如下写法都可以:

                  TYPE const ValueName = value;
                 const TYPE ValueName = value;

  2 当作为全局变量并且在他文件也想使用时:(默认const具有局部性)

           1.CPP  :extern const TYPE ValueName = value;定义

           2.CPP:  extern const  TYPE ValueName; 外部声明

    上面两个const 和TYPE可以颠倒。

  3 指针使用const

         如果const位于*的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;
         如果const位于*的右侧,const就是修饰指针本身,即指针本身是常量。

   4 函数参数中返回值中用const

   5 类与const:
    class A
    {
        …
        const int nValue;        //成员常量不能被修改
        …
        A(int x): nValue(x) { } ; //只能在初始化列表中赋值

        void function()const; //常成员函数, 它不改变对象的成员变量.                       

                                      //也不能调用类中任何非const成员函数。


     }

       1)修饰成员变量:表示成员常量,不能被修改,同时它只能在初始化列表中赋值。

       2)const成员函数不被允许修改它所在对象的任何一个数据成员,函数中可以使用非const成员变量,但不能修改。 不能调用类中任何非const成员函数。(因为任何非const成员函数会有修改成员变量的企图。const成员函数声明和定义const都不能少

       3)const类对象、指针、引用:只能调用类的const成员函数,可以使用成员变量,但不能改变值。因此,const修饰成员函数的最重要作用就是限制对于const对象的使用。

 

class A
{ 
public:    
   const int nValue;        //成员常量不能被修改
   int m_a;
    
   A(int x,int a): nValue(x) { m_a=a;} ; //只能在初始化列表中赋值
    
   void func()const; //常成员函数, 它不改变对象的成员变量.                       
    
    //也不能调用类中任何非const成员函数。    
}; 

void A::func() const
{
    cout<<m_a<<endl;
    cout<<nValue<<endl;
}
int main()
{
    const A aa(1,2);
    cout<<aa.m_a<<endl;
    aa.func();
    return 0;
}

补充:const与宏区别:

#define PI 3.14159         //常量宏
const doulbe  Pi=3.14159;  //此时并未将Pi放入ROM中
              ......
double i=Pi;   //此时为Pi分配内存,以后不再分配!
double I=PI;  //编译期间进行宏替换,分配内存
double j=Pi;  //没有内存分配
double J=PI;  //再进行宏替换,又一次分配内存!

 

static:

 

class MyTestClass  
{  
    public:  
        MyTestClass() : m_ciInt(1), m_csStr("MyStr")  // const成员变量,在ctor参数列表中初始化  
            {}  
    public:  
        const int m_ciInt;  
        const string m_csStr;  
        static int m_siInt;  
        static string m_ssStr;  
        const static int m_csiInt;  
        const static string m_cssStr; 
        
        static void func();
};  
    int MyTestClass::m_siInt = 1; // static成员变量,在外部定义  
    string MyTestClass::m_ssStr = "MyStr"; // static成员变量,在外部定义  
    const int MyTestClass::m_csiInt = 1;  // const static/static const成员变量,在外部定义  
    const string MyTestClass::m_cssStr = "MyStr"; // const static/static const成员变量,在外部定义

     void MyTestClass::func()
    {
        cout<<m_siInt<<m_ssStr<<m_csiInt<<m_cssStr<<endl;
    }
    int main()
    {
        MyTestClass myclass;
        myclass.func();
        MyTestClass::func();
        return 0;

    }

 

一般这么初始化就可以了,高版本编译器支持类声明中 const static int m_csiInt=5;//常量声明式。

通常C++要求你对所使用的任何东西提供一个定义式,但如果是类的const static 且为整数类型(int,char,bool)
则可以“in_class初值设定”。只要后续使用中不取它们的地址,就可以这么声明后无需提供定义式。如果需要定义式,在类外:

const int MyTestClass::m_csiInt;//不可以再设初值。

注意:static成员变量外部定义不可以出现static关键字。

静态变量和静态函数存在的意义?

 

答:类中静态函数意义:(类外定义不能写static关键字

 

     1静态私有成员在类外不能被访问,可通过类的静态成员函数来访问;

 

当类的构造函数是私有的时,不像普通类那样实例化自己,只能通过静态成员函数来调用构造函数。(单例模式)

 

普通静态函数,静态变量作用:限定了只能在本文件中使用。

 

静态成员变量和静态成员函数都是为类所拥有,只有一份。静态变量保存在静态存储区。

 

inline:C++类中使用

三种内联函数形式:1 默认内联函数 2 显示定义内联函数 3 类外定义为内联函数

class Student
{
  private:
    int nID
int nAge;
    float fScore;
  public:
    void setID(int nid){ nID = nid; }    //该成员函数默认自动为内联函数(隐式定义内联函数)
    inline void setAge(int nage) { nAge = nage; }   //显式定义内联函数
    void setScore(float fscore);         //类定义体内没有声明为内联函数;
}
inline void Student::setScore(float fscore){ fScore = fscore; } //类定义体外实现为内联函数;

由于内联函数是编译器在便宜阶段进行函数体展开的,所以,这就把类的内联函数的定义与实现都必须在声明类的那个头文件中,而不能放在实现类的那个cpp文件中;这一点与模板template<>的特性相似;

 

 

 

 

 

      

 

posted on 2014-08-15 11:25  Yogurshine  阅读(565)  评论(0编辑  收藏  举报