不能重载的运算符的作用及叫法啊

buduan0 2008-11-06 08:46:23
其中有一种为 ::(叫:作用区分符域,是C++的,如)其它的
.* .-> . 都怎么解释和作用呀?最好举个例.
...全文
97 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhaohu 2008-11-07
  • 打赏
  • 举报
回复
.-> . ,其实就是指针与引用
帅得不敢出门 2008-11-07
  • 打赏
  • 举报
回复
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 Code
// 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

m_func1
m_func1
1
2


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 Code
// expre_Expressions_with_Pointer_Member_Operators2.cpp
// C2440 expected
class 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.

buduan0 2008-11-07
  • 打赏
  • 举报
回复
有书还出来问啊!!就是因为没书好不好.
Big鹏 2008-11-07
  • 打赏
  • 举报
回复

《C++ PRIMER》第四版
Super.Jiju 2008-11-06
  • 打赏
  • 举报
回复
帮顶
接分
R9R9R9 2008-11-06
  • 打赏
  • 举报
回复
兄弟看看C++ Primer吧,这书讲的估计比这里所有人说的都清楚.

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧