c++基本使用(const修饰成员函数和this指针)

这里写目录标题

const修饰成员函数

    void show1() const
    { 
        //m_name="西施show1";
        cout << "姓名:" << m_name << ",年龄:" << m_age << endl; 
    }

mutable可以突破const的限制,被mutable修饰的成员变量,将永远处于可变的状态,在const修饰的函数中,mutable成员也可以被修改。

mutable string    m_name;                     // 姓名属性。
int         m_age;                         // 年龄属性。

void show1() const
{ 
    m_name="西施show1";
    cout << "姓名:" << m_name << ",年龄:" << m_age << endl; 
}
  1. mutable可以突破const的限制,被mutable修饰的成员变量,将永远处于可变的状态,在const修饰的函数中,mutable成员也可以被修改。
  2. 非const成员函数可以调用const成员函数和非const成员函数。
  3. const成员函数不能调用非const成员函数。
  4. 非const对象可以调用const修饰的成员函数和非const修饰的成员函数。
  5. const对象只能调用const修饰的成员函数,不能调用非cosnt修饰的成员函数。
#include <iostream>         // 包含头文件。
using namespace std;        // 指定缺省的命名空间。

class CGirl                 // 超女类CGirl。
{
public:
    mutable string    m_name;                     // 姓名属性。
    int         m_age;                         // 年龄属性。

    // 两个参数的普通构造函数。
    CGirl(const string &name, int age) 
    { m_name = name; m_age = age;  cout << "调用了CGirl(name,age)构造函数。\n"; }
    
    // 超女自我介绍的方法,显示姓名、年龄。
    void show1() const
    { 
        m_name="西施show1";
        cout << "姓名:" << m_name << ",年龄:" << m_age << endl; 
    }
    void show2() const
    {
        m_name = "西施show2";
        cout << "姓名:" << m_name << ",年龄:" << m_age << endl;
    }
    void show3() 
    {
        m_name = "西施show3";
        cout << "姓名:" << m_name << ",年龄:" << m_age << endl;
    }
    void show4() 
    {
        m_name = "西施show4";
        cout << "姓名:" << m_name << ",年龄:" << m_age << endl;
    }
};

int main()
{
    const CGirl g1("冰冰",18);
    
    g1.show2();
}

this指针

如果类的成员函数中涉及多个对象,在这种情况下需要使用this指针。

this指针存放了对象的地址,它被作为隐藏参数传递给了成员函数,指向调用成员函数的对象(调用者对象)。

    const CGirl& pk(const CGirl& g) const
    {
        if (g.m_yz < m_yz) return g;
        //谁调用了pk函数,this指针就指向谁
        //从类的成员函数返回自己
        return *this;
    }
#include <iostream>         // 包含头文件。
using namespace std;        // 指定缺省的命名空间。

class CGirl                 // 超女类CGirl。
{
public:
    string    m_name;   // 姓名属性。
    int         m_yz;   // 颜值:1-沉鱼落雁;2-漂亮;3-一般;4-歪瓜裂枣。

    // 两个参数的普通构造函数。
    CGirl(const string &name, int yz)  { m_name = name; m_yz = yz;  }
    
    // 超女自我介绍的方法。
    void show() const  { cout << "我是:" << m_name << ",最漂亮的超女。"<< endl; }

    // 超女颜值pk的方法。
    const CGirl& pk(const CGirl& g) const
    {
        if (g.m_yz < m_yz) return g;
        //谁调用了pk函数,this指针就指向谁
        //从类的成员函数返回自己
        return *this;
    }
};

int main()
{
    // 比较五个超女的颜值,然后由更漂亮的超女作自我介绍。
    CGirl g1("西施",5), g2("西瓜",3), g3("冰冰", 4), g4("幂幂", 5), g5("金莲", 2);
    //类形参改为引用,类成员改为引用(都不调用构造函数了)
    const CGirl& g = g1.pk(g2).pk(g3).pk(g4).pk(g5);
    g.show();
}

每个成员函数(包括构造函数和析构函数)都有一个this指针,可以用它访问调用者对象的成员。(可以解决成员变量名与函数形参名相同的问题)

void func(int aa){
    this.aa = aa;
}

*this可以表示对象。(this指针的解引用)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值