Type of 'this' pointer in C++
2013-11-26 09:37
423 查看
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
#include<iostream> class X { void fun() const { // this is passed as hidden argument to fun(). // Type of this is const X* } };
Code 2
#include<iostream> class X { void fun() volatile { // this is passed as hidden argument to fun(). // Type of this is volatile X* } };
Code 3
#include<iostream> class X { void fun() const volatile { // this is passed as hidden argument to fun(). // Type of this is const volatile X* } };
补充:
(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
相关文章推荐
- 'this' pointer in C++
- Virtual method and base-type pointer make polymorphism in C++
- C++ 指针的偏移 The offset of a pointer in C++
- Implementing a simple smart pointer in C++
- Difference between pointer variable and reference variable in C++
- Effective C++ Item 17 Store newed objects in smart pointer in standalone statements
- The differences between pointer and reference in C++
- Jump Tables via Function Pointer Arrays in C/C++
- ‘this’ pointer in C++
- Pointer in C/C++
- The two-dimensional pointer operation in C++
- The pointer in C++ /CLI
- Implementing a simple smart pointer in c++
- Implementing a simple smart pointer in C++
- What are the differences between a pointer variable and a reference variable in C++?
- ‘this’ pointer in C++
- What are the differences between a pointer variable and a reference variable in C++?
- C++'s Function pointer and C#'s delegate
- 【ThinkingInC++】2、输入输出流
- java.lang.NullPointerException: Expected timestamp in the Flume event headers, but it was null