看下面一段程序:
#include <iostream.h>
class a
{
public :
void print() const {cout <<"a::print() called\n" ;};
};class b :public a
{
public :
void f(){} ;
void printf() {cout <<"b::printf() called\n" ;};
};void f1(const a& r)
{
r.print() ;
};void main()
{
b b1 ;
f1(b1) ;
return ;
};
请问:
 程序输出结果是什么. 
对于void print() const {cout <<"a::print() called\n" ;}; 这段程序中的const 看不明白..请各位高手讲解讲解........讲解明确者,马上结贴,所有分只送一个人.

解决方案 »

  1.   


       print()为void型,const应该没什么用途吧.
      

  2.   

    a::print() called常成员函数,表示常对象可以调用这个函数(当然非常对象也可以)。不加const的成员函数,常对象是为能调用的。常成员函数不能访问类的非常非静态成员。
      

  3.   

    用const修饰函数表示不能在这个函数那修改类的成员,也就是说这个函数内的this指针是const的(只读)
      

  4.   

    结果应该是:a::print() called
    void print() const {cout <<"a::print() called\n" ;};
    是:
    const成员函数,只有const成员函数才能访问const对象,当然const成员函数特不能修改const对象的
    在写程序过程,最好把const对象所要使用的所有成员函数都声明为const.详细情况可以参考
    H.M.Deitel和P.J.Deitel父子写的<<c++程序教程>>第三章.
      

  5.   


    结果是:a::print() called
    void print() const  "const"在此处作用不大,这样写是为了类的成员函数能够访问私有定义的常量