首页 > 代码库 > 对象指针与this指针

对象指针与this指针

对象指针分为三大类

【1】指向对象的指针

【2】指向对象成员的指针(数据类)

【3】指向对象成员的指针(函数类)

#include<iostream>using namespace std;class Time{        public :                Time(int,int,int);                void get_time();        private:                int hour;                int minute;                int sec;:};Time::Time(int h,int m ,int s){        hour = h;        minute = m;        sec = s;}void Time::get_time(){        cout<<hour<<":"<<minute<<":"<<sec>>endl;}int main(){        Time t1(10,13,56);        int *p1 = &t1.hour;        cout<<*p1<<endl;        t1.get_time();        Time *p2 = &t1;        p2->get_time();        void(Time::*p3)();        p3 = &Time::get_time;        (t1.*p3)();}~      

 

this指针

  在每一个成员函数中都包含一个特殊的指针,这个
  this指针的名字是固定的,称为this它是指向本类对
  象的指针,它的值是当前被调用的成员函数所在的
  对象的起始地址。

int Box∷volume( ){  
  return (height*width*length);}C++把它处理为int Box volume(Box *this){
  return(this->height * this->width * this->length);}