注册 登录
编程论坛 C++教室

c++指针问题

ackie 发布于 2007-09-20 11:56, 524 次点击

//---------------------------------------------------------------------------
#include<iostream.h>
#include <vcl.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
class simplecat
{
public:
simplecat();
simplecat(simplecat&);
~simplecat();
int getage() const {return itsage;}
int setage(int age){itsage=age;}
private:
int itsage;
};
simplecat::simplecat()
{
cout<<"simple cat constructor..."<<endl;
itsage=1;
}
simplecat::simplecat(simplecat&)
{
cout<<"simple cat copy constructor..."<<endl;
}
simplecat::~simplecat()
{
cout<<"simple cat destructor..."<<endl;
}
const simplecat * const functiontwo(const simplecat *const thecat);
int main(int argc, char* argv[])
{
cout<<"making a cat..."<<endl;
simplecat frisky;
cout<<"frisky is";
cout<<frisky.getage();
cout<<"years old"<<endl;
int age=5;
frisky.setage(age);
cout<<"frisky is";
cout<<frisky.getage();
cout<<"years old"<<endl;
cout<<"calling functiontwo.... "<<endl;
functiontwo(&frisky);
cout<<"frisky is";
cout<<frisky.getage();
cout<<"years old"<<endl;
char response;
cin>>response;

return 0;
}
const simplecat *const functiontwo(const simplecat *const thecat)
{
cout<<"function two.returning..."<<endl;
cout<<"frisky is now"<<thecat->getage();
cout<<"years old"<<endl;
thecat->setage(8);
return thecat;
}
//---------------------------------------------------------------------------
蓝色那一行代码是什么意思??为什么函数前面要*??
那位高手给解释下,谢谢!!

4 回复
#2
aipb20072007-09-20 16:08
const TYPE *p
TYPE const *p
指针p指向一个常量

TYPE *const p
常量指针

const TYPE *const p
指向常量的常量指针
#3
ackie2007-09-20 19:03
const functiontwo前面的*是什么意思??
#4
aipb20072007-09-20 21:55
const simplecat * const

看做一个整体,是返回值,类型去查看2楼
#5
jonc2007-11-02 17:17

const simplecat * const functiontwo(const simplecat *const thecat);
中第一个const表示常量数据
第二个const表示常量函数指针
第三个const表示常量数据
第四个表示常量指针

1