码迷,mamicode.com
首页 > 其他好文 > 详细

对象指针与this指针

时间:2014-08-04 10:36:36      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   数据   div   amp   

对象指针分为三大类

【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); }

 

对象指针与this指针,布布扣,bubuko.com

对象指针与this指针

标签:style   blog   color   os   io   数据   div   amp   

原文地址:http://www.cnblogs.com/fengdashen/p/3889334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!