C++基础:共享方法如何识别对象,*this

来源:互联网 发布:淘宝固定代码 编辑:程序博客网 时间:2024/05/22 10:37
编译器对类的识别顺序:
1.识别类名;

2.识别数据成员;

        3.识别函数,改写函数;

什么是*this:

        1.代表当前对象;
        2.当函数被调用时,this指针会被隐藏插入;

改写方法:

        1.函数改写:
改写前:

void RegisterGoods(char name[],int amount,float price);
{
strcpy(Name,name);
Amount = amount;
Price = price;
}
改写为:

void RegisterGoods(CGoods *const this,char name[],int amount,float price);
{
strcpy(this->Name,name);
this->Amount = amount;
this->Price = price;
}
 2.调用改写:
改写前:c1.RegisterGoods("C++",10,12);
改写为:c1.RegisterGoods(&c1,"C++",10,12);

0 0