• Type of 'this' pointer in C++


     

      

      In C++, this pointer is passed as a hidden argument to all non-static member function calls. The type of this depends upon function declaration. If the member function of a class X is declared const, the type of this is const X* (see code 1 below), if the member function is declared volatile, the type of this is volatile X* (see code 2 below), and if the member function is declared const volatile, the type of this is const volatile X* (see code 3 below).

      Code 1

    1 #include<iostream>
    2 class X 
    3 {
    4    void fun() const 
    5    {
    6         // this is passed as hidden argument to fun(). 
    7         // Type of this is const X* 
    8    }
    9 };

     

      Code 2

    1 #include<iostream>
    2 class X 
    3 {
    4    void fun() volatile 
    5    {
    6         // this is passed as hidden argument to fun(). 
    7         // Type of this is volatile X* 
    8     }
    9 };

     

      Code 3

    1 #include<iostream>
    2 class X 
    3 {
    4    void fun() const volatile 
    5    {
    6      // this is passed as hidden argument to fun(). 
    7      // Type of this is const volatile X* 
    8     }
    9 };

     

      补充:

      (1)In an ordinary nonconst member function, the type of this is a const pointer to the class type. We may change the value to which this points but cannot change the address that this holds. In a const member function, the type of this is a const pointer to a const class - type object. We may change neither the object to which this points nor the address that this holds.

      (2)What is the purpose of a volatile member function in C++?【from stackoverflow】

      

     

     

      References:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf

     

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
          

      转载请注明:http://www.cnblogs.com/iloveyouforever/  

      2013-11-26  09:32:58

  • 相关阅读:
    金融法规
    大型网站架构演化
    用一张决策图来重新思考你的网站
    互联网产品的一般商业模式
    怎样开始冥想
    创造安宁舒适工作日的17个办法
    人生规划和GTD——"知"、"得"与"合"
    立即行动的七个方法
    文件归档的学问
    入门GTD时间管理系统必读
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442675.html
Copyright © 2020-2023  润新知