首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于种中的const

2013-01-11 
关于类中的const看到一个这样的定义,class sT{public:double avg() const...}想知道这个定义究竟是什么意

关于类中的const
看到一个这样的定义,
class sT{
      public:
             double avg() const;
       ...
}
想知道这个定义究竟是什么意思呢??
请详细给个解释吧,多谢了。 public
[解决办法]
先说一句楼主基础知识堪忧哇~~
一般来说,关键字const放在形参表之后,就可以将成员函数声明为常量:
double avg() const;
const成员不能改变其所操作的对象的数据成员。const必须同时出现在声明和定义中,若只出现在一处,就会有一个编译时刻的错误。
不知道楼主懂否?

[解决办法]
补充一下知识点:
const用在成员函数后   主要是针对类的const 对象  
  如:  
  class   Text{  
  public:  
         void   printconst(void)  const
                              {cout<<"hello"<<endl;}  
          void   print(void)
                          {cout<<"hello"<<endl;}  
  private:  
          int   k;  
  };  
 const   Text   a;  
  //上面定义了类Text的一常量对象  
  int   main(void)  
  {  
          a.printconst();   //ok  
          a.print();             //error      
          //上面a.print()调用是非法的  
          return   0;  
  }  
     const对象只能调用const成员函数。 上例中const对象a,只能调用其中的const成员函数。 
      const对象的值不能被修改,在const成员函数中修改const对象数据成员的值是语法错误  
      在const函数中调用非const成员函数是语法错误  
        这是把整个函数修饰为const,意思是“函数体内不能对成员数据做任何改动”。如果你声明这个类的一个const实例,那么它就只能调用有const修饰的成员函数。
 常成员函数   
         使用const关键字进行说明的成员函数,称为常成员函数。只有常成员函数才有资格操作常量或常对象,没有使用const关键字说明的成员函数不能用来操作常对象。常成员函数说明格式如下:   
        <类型说明符>   <函数名>   (<参数表>)   const;  
        其中,const是加在函数说明后面的类型修饰符,它是函数类型的一个组成部分,因此,在函数实现部分也要带const关键字。下面举一例子说明常成员函数的特征。   
  #include  
  class   R  
  {  
  public:   
                      R(int   r1,   int   r2)   {   R1=r1;   R2=r2;   }  
                     void   print();  


                    void   print()   const;  
  private:  
                     int   R1,   R2;  
  };   
   void   R::print()  
  {  
                        cout<<R1<<R2<<endl;  
  }   
    void   R::print()   const  
  {  
                    cout<<R1<<R2<<endl;  
  }   
   void   main()  
  {  
  R   a(5,   4);  
  a.print();  
  const   R   b(20,   52);  
  b.print();  
  }   
    该例子的输出结果为:   
      5,4  
    20;52   
     该程序的类声明了两个成员函数,其类型是不同的(其实就是重载成员函数)。有带const修饰符的成员函数处理const常量,这也体现出函数重载的特点。 
另外还有一些问题须要说明:
在C++中,函数名前的const和函数名后的const有区别吗?
有区别。
首先要知道函数名后面加const只能用于成员函数。
int const func(); //合法,相当于const int func();
int func2() const; //非法,对函数的const限定词只能用于成员函数
在成员函数中,const加在函数名前和后也是有区别的。
例如:
class A {
    int const func();
    int func() const;
};
int const A::func() { return 0; }
int A::func() const { return 0; }
     上面的代码是合法的,其中A::func成员函数是一个重载成员函数,两个函数都返回int类型数据(注意:对于C/C++,返回类型或参数类型中,const int和int被认为是一种类型。但是const int *和int *不是一种类型),这两个重载函数正是基于函数名后的const来重载的。
    int const func();表示该成员函数的隐藏this指针参数是A * const类型的;而int func() const;表示该重载成员函数的隐藏this指针参数是A const * const类型的.
A * const类型和A const * const类型是不同类型,因此可以重载。
由此可见const放在函数名后和名前是不同的
[解决办法]
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. 

A constant member function cannot modify any data members or call any member functions that aren't constant.

鉴于上面的原因。You cannot declare constructors or destructors with the const keyword.

You can call constant member functions only for a constant object. This ensures that the object is never modified.



You can also overload a member function using the const keyword; this allows a different version of the function to be called for constant and nonconstant objects.


#include <iostream>
using namespace std;

class foo
{
public:
void print()
{
cout<<"print called"<<endl;
}
void print() const
{
cout<<"const print called"<<endl;
}
};

int main()
{
foo a;
a.print();

const foo b;
b.print();


return 0;
}

输出结果为:
print called
const print called

热点排行