[C++之旅] 16 对象指针/对象成员指针/this指针

[C++之旅] 16 对象指针/对象成员指针/this指针

一、对象指针

  • 对象指针指向堆中地址
类名 *对象指针 = new 类名();//或者类名 *对象指针 = NULL; 对象指针 = new 类名;
delete 对象指针;
对象指针 = NULL;
  • 对象指针指向栈中地址
类名 对象;
类名 *对象指针 = &对象;
delete 对象指针;
对象指针 = NULL;

访问对象指针的成员有两种方式如下:

对象指针->成员/函数...
(*对象指针).成员/函数...

二、对象成员指针

即对象成员采用new运算符实例化,实例化一个包含对象成员指针的对象时,其对象所占用的内存为对象中成员+对象指针本身的大小,并不包含对象指针申请的堆中内存,因此在执行该对象的析构函数时不会自动释放堆中内存,需用户在析构函数中自行释放,其释放内存的顺序为先释放对象指针堆中的内存再将对象本身的内存释放。

class Coordinate
{
public:
    Coordinate(int x, int y):
    m_x(x), m_y(y){}
    
private:
    int m_x;
    int m_y;
};

Coordinate表示点的类,其中包含点的xy坐标

class Line
{
public:
    Line(int x1, int y1, int x2, int y2);

private:
    Coordinate *m_coorA;
    Coordinate *m_coorB;
};

Line::Line(int x1, int y1, int x2, int y2)
{
    m_coorA = new Coordinate(x1, y1);
    m_coorB = new Coordinate(x2, y2);
}

Line::~Line()
{
    delete m_coorA;
    m_coorA = NULL;
    delete m_coorA;
    m_coorA = NULL;
}

Line表示线的类,包含AB两个坐标点的指针,使用对象成员指针时一定要在析构函数中释放掉

int main(void)
{
    Line *p = new Line(1, 2, 4, 5);
    
    delete p;
    p = NUll;
    return 0;
}

三、this指针

class Coordinate
{
public:
    Coordinate(int x, int y):
    m_x(x), m_y(y){}
    void setpoint(x, y);
private:
    int m_x;
    int m_y;
};

以坐标这个类为例,在实例化三个点时,会在内存中新建三个数据成员,而成员函数却只有一份存在代码区。三个对象只有一个成员函数,那在调用成员函数时,编译器是怎么知道操作的是哪个对象的数据成员呢?其实每个成员函数中都包含this指针,作为入口参数,编译器根据入口的指针分辨使用哪个数据成员,其函数原型如下,为例方便 C++编译器将this“省略”了,其实在编译时编译器自动为每一个成员函数的参数列表加了一个this 指针。

class Coordinate
{
public:
    Coordinate(T *this, int x, int y):
    m_x(x), m_y(y){}
    void setpoint(T *this, x, y);
private:
    int m_x;
    int m_y;
};

int main()
{
    Coordinate coorA(this, 1, 3);
    coorA.setpoint(this, 3, 4);
    Coordinate coorB(this, 1, 3);
    coorB.setpoint(this, 3, 4);
    return 0;
}
  • this解决参数名与成员数据名同名问题

    上面理解了this指针,那this指针在c++中怎么使用呢?如下实例setpoint函数的输入参数与成员变量名相同,在执行赋值语句时编译器无法分清哪个时参数哪个时成员变量,此时在前面加入this指针可以让编译器知道为成员变量。

    class Coordinate
    {
    public:
      Coordinate(int x, int y):
        m_x(x), m_y(y){}
      void setpoint(m_x, m_y);
    private:
      int m_x;
      int m_y;
    };
    
    void Coordinate::setpoint(m_x, m_y)
    {
        this->m_x = m_x;
        this->m_y = m_y;
    }
    
    int main()
    {
        Coordinate coorA(1, 3);
        coorA.setpoint(3, 4);
        Coordinate coorB(1, 3);
        coorB.setpoint(3, 4);
        return 0;
    }
    
  • this作为返回值

    将setpoint函数进行修改,返回this对象(this为指针,*this为对象),也就是返回当前对象。

    Coordinate& Coordinate::setpoint(m_x, m_y)
    {
        this->m_x = m_x;
        this->m_y = m_y;
        return *this;
    }
    
    int main()
    {
        Coordinate coorA(1, 3);
        coorA.setpoint(3, 4).setpoint(5, 6);
        Coordinate coorB(1, 3);
        coorB.setpoint(3, 4);
        return 0;
    }
    

    其最终coorA的坐标为(5,6)。因为返回的为this对象的引用,其实返回的就是coorA这个对象。若将setpoint改为:

    Coordinate Coordinate::setpoint(m_x, m_y)
    {
        this->m_x = m_x;
        this->m_y = m_y;
        return *this;
    }
    

    则不会修改coorA的成员数据,此时返回的是一个新的Coordinate的对象,操作的并不少coorA了。

    除了采用引用的方式,还可以采用指针的方式,如下:

    Coordinate* Coordinate::setpoint(m_x, m_y)
    {
        this->m_x = m_x;
        this->m_y = m_y;
        return this;
    }
    
    int main()
    {
        Coordinate coorA(1, 3);
        coorA.setpoint(3, 4)->setpoint(5, 6);
        Coordinate coorB(1, 3);
        coorB.setpoint(3, 4);
        return 0;
    }
    

    这种方式也是可以的,只是在利用指针的形式访问成员变量时,需使用->符号。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容