Pointer-to-Member Operators: .* and ->*
2012-12-18 15:00
239 查看
Visual Studio 2012
Other Versions
Visual Studio 2010
Visual Studio 2008
Visual Studio 2005
Visual Studio .NET 2003
This topic has not yet been rated
- Rate this topic
Copy
expression .* expression expression –>* expression
Remarks
The pointer-to-member operators, .* and –>*, return the value of a specific class member for the object specified on the left side of the expression. The right side must specify a member of the class. The following example shows how
to use these operators:
Copy
// expre_Expressions_with_Pointer_Member_Operators.cpp// compile with: /EHsc#include <iostream>using namespace std;class Testpm {public: void m_func1() { cout << "m_func1\n"; } int m_num;};// Define derived types pmfn and pmd.// These
types are pointers to members m_func1() and// m_num, respectively.void (Testpm::*pmfn)() = &Testpm::m_func1;int Testpm::*pmd = &Testpm::m_num;int main() { Testpm ATestpm; Testpm *pTestpm = new Testpm;// Access the member function (ATestpm.*pmfn)(); (pTestpm->*pmfn)();
// Parentheses required since * binds // less tightly than the function call.// Access the member data ATestpm.*pmd = 1; pTestpm->*pmd = 2; cout << ATestpm.*pmd << endl << pTestpm->*pmd << endl; delete pTestpm;}
Output
Copy
m_func1m_func112
In the preceding example, a pointer to a member,
pmfn, is used to invoke the member function m_func1. Another pointer to a member,
pmd, is used to access the m_num member.
The binary operator .* combines its first operand, which must be an object of class type, with its second operand, which must be a pointer-to-member type.
The binary operator –>* combines its first operand, which must be a pointer to an object of class type, with its second operand, which must be a pointer-to-member type.
In an expression containing the .* operator, the first operand must be of the class type of, and be accessible to, the pointer to member specified in the second operand or of an accessible type unambiguously derived from and accessible
to that class.
In an expression containing the –>* operator, the first operand must be of the type "pointer to the class type" of the type specified in the second operand, or it must be of a type unambiguously derived from that class.
Example
Consider the following classes and program fragment:
Copy
// expre_Expressions_with_Pointer_Member_Operators2.cpp// C2440 expectedclass BaseClass {public: BaseClass(); // Base class constructor. void Func1();};// Declare a pointer to member function Func1.void (BaseClass::*pmfnFunc1)() = &BaseClass::Func1;class
Derived : public BaseClass {public: Derived(); // Derived class constructor. void Func2();};// Declare a pointer to member function Func2.void (Derived::*pmfnFunc2)() = &Derived::Func2;int main() { BaseClass ABase; Derived ADerived; (ABase.*pmfnFunc1)(); //
OK: defined for BaseClass. (ABase.*pmfnFunc2)(); // Error: cannot use base class to // access pointers to members of // derived classes. (ADerived.*pmfnFunc1)(); // OK: Derived is unambiguously // derived from BaseClass. (ADerived.*pmfnFunc2)(); // OK: defined
for Derived.}
The result of the .* or –>* pointer-to-member operators is an object or function of the type specified in the declaration of the pointer to member. So, in the preceding example, the result of the expression
ADerived.*pmfnFunc1() is a pointer to a function that returns void. This result is an l-value if the second operand is an l-value.
这么厉害的东西可以用来做回调函数用哦!!!还能做什么没有想到!
Other Versions
Visual Studio 2010
Visual Studio 2008
Visual Studio 2005
Visual Studio .NET 2003
This topic has not yet been rated
- Rate this topic
Copy
expression .* expression expression –>* expression
Remarks
The pointer-to-member operators, .* and –>*, return the value of a specific class member for the object specified on the left side of the expression. The right side must specify a member of the class. The following example shows how
to use these operators:
Copy
// expre_Expressions_with_Pointer_Member_Operators.cpp// compile with: /EHsc#include <iostream>using namespace std;class Testpm {public: void m_func1() { cout << "m_func1\n"; } int m_num;};// Define derived types pmfn and pmd.// These
types are pointers to members m_func1() and// m_num, respectively.void (Testpm::*pmfn)() = &Testpm::m_func1;int Testpm::*pmd = &Testpm::m_num;int main() { Testpm ATestpm; Testpm *pTestpm = new Testpm;// Access the member function (ATestpm.*pmfn)(); (pTestpm->*pmfn)();
// Parentheses required since * binds // less tightly than the function call.// Access the member data ATestpm.*pmd = 1; pTestpm->*pmd = 2; cout << ATestpm.*pmd << endl << pTestpm->*pmd << endl; delete pTestpm;}
Output
Copy
m_func1m_func112
In the preceding example, a pointer to a member,
pmfn, is used to invoke the member function m_func1. Another pointer to a member,
pmd, is used to access the m_num member.
The binary operator .* combines its first operand, which must be an object of class type, with its second operand, which must be a pointer-to-member type.
The binary operator –>* combines its first operand, which must be a pointer to an object of class type, with its second operand, which must be a pointer-to-member type.
In an expression containing the .* operator, the first operand must be of the class type of, and be accessible to, the pointer to member specified in the second operand or of an accessible type unambiguously derived from and accessible
to that class.
In an expression containing the –>* operator, the first operand must be of the type "pointer to the class type" of the type specified in the second operand, or it must be of a type unambiguously derived from that class.
Example
Consider the following classes and program fragment:
Copy
// expre_Expressions_with_Pointer_Member_Operators2.cpp// C2440 expectedclass BaseClass {public: BaseClass(); // Base class constructor. void Func1();};// Declare a pointer to member function Func1.void (BaseClass::*pmfnFunc1)() = &BaseClass::Func1;class
Derived : public BaseClass {public: Derived(); // Derived class constructor. void Func2();};// Declare a pointer to member function Func2.void (Derived::*pmfnFunc2)() = &Derived::Func2;int main() { BaseClass ABase; Derived ADerived; (ABase.*pmfnFunc1)(); //
OK: defined for BaseClass. (ABase.*pmfnFunc2)(); // Error: cannot use base class to // access pointers to members of // derived classes. (ADerived.*pmfnFunc1)(); // OK: Derived is unambiguously // derived from BaseClass. (ADerived.*pmfnFunc2)(); // OK: defined
for Derived.}
The result of the .* or –>* pointer-to-member operators is an object or function of the type specified in the declaration of the pointer to member. So, in the preceding example, the result of the expression
ADerived.*pmfnFunc1() is a pointer to a function that returns void. This result is an l-value if the second operand is an l-value.
Note |
---|
If the result of one of the pointer-to-member operators is a function, then the result can be used only as an operand to the function call operator. |
相关文章推荐
- Pointer-to-Member Operators: .* and ->*
- Pointer-to-Member Operators: .* and ->*
- 【小熊刷题】Best Time to Buy and Sell Stock I <leetcode 121 Java>
- How to include <android/bitmap.h> and other *.h NDK libraries to Titanium Android module?
- <<Beginning XML with DOM and Ajax From Novice to Professiona>>Contents
- web.xml is missing and <failOnMissingWebXml> is set to true[解决]
- web.xml is missing and <failOnMissingWebXml> is set to true
- maven项目出现web.xml is missing and <failOnMissingWebXml> is set to true错误的解决办法
- poe.xml 出现web.xml is missing and <failOnMissingWebXml> is set to true的错误
- unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make a
- 2>&1 and 1>&2
- hdu 1069 Monkey and Banana -->dp
- Robbie's Notes on File System Security in Linux (and comparison to Windows NT)
- pom.xml提示web.xml is missing and <failOnMissingWebXml>is set to true
- !!![YOU SHOULD NEVER EVER BE SO FOOL TO MAP <C-N> TO <C-X><C-O>]!!!
- adb报错:The connection to adb is down, and a severe&nbs
- Top JavaScript Frameworks, Libraries & Tools and When to Use Them
- Table 9-1 -- Bit strings with "prefix" and "suffix" bits and assigment to codeNum rangs
- sql关系运算符优先级and>or