Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368945
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-22 10:00:36

The this pointer is used as a pointer to the object instance by the member function. The address of the class instance is passed as an implicit parameter to the member functions. The sample below, in this c++ Tutorial shows how to use it. It is a common knowledge that C++ keeps only one copy of each member function and the data members are allocated memory for all of their instances. This kind of various instances of data are maintained use this pointer. Look at the sample below, in this c++ Tutorial.
C++ Tutorial - important notes on this pointer:
  • this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
  • this pointer is not counted for calculating the size of the object.
  • this pointers are not accessible for static member functions.
  • this pointers are not modifiable.

  Look at the following example to understand how to use the 'this' pointer explained in this C++ Tutorial. 

    class this_pointer_example // class for explaining C++ tutorial 
    {
       int data1;
    public:
       //Function using this pointer for C++ Tutorial
       int getdata()
       { 
           return this->data1;
       } 
     //Function without using this pointer 
     void setdata(int newval)
     {
          data1 = newval;
     }
};
    
  Thus, a member function can gain the access of data member by either using this pointer or not.

阅读(1319) | 评论(1) | 转发(0) |
0

上一篇:重载函数

下一篇:const 数据 指针

给主人留下些什么吧!~~

onezeroone2011-04-24 15:59:07

only one copy of each member function and the data members are allocated memory for all of their instances.