在这种情况下,哪个操作员被调用
Which operator is called in that case
有这样的结构时:
Foo f;
f->bar(); //Here is called the class member access operator
但是,当'f'是指向foo类型对象的指针时:
Foo* f = new Foo();
(*f)->bar(); //Here is also called the class member access operator
f->bar(); //<-- Which operator is called?
//Is it the pointer to member one (->*),
//or is the pointer-dereference one, or maybe both of them?
我还想问这个行为可以超负荷吗?
...
class Foo{
...
Foo* operator->() const{
cout << "overloaded" << endl;
return this;
}
};
Foo a;
Foo* b = new Foo();
a->bar(); //Here is called the overloaded ->
(*b)->(); //Again the overloaded one
b->bar(); //This calls something else
您不能在指针上超载->
操作员。当p
是指针时,p->bar()
将取消指针,并在对象上调用bar
功能。它不会调用任何超载运营商。
另一方面,(*p)->bar()
将在指向类型上调用Overloaded ->
操作员。
相关文章:
- 为什么STD :: SPAN超载函数调用操作员索引
- C 操作员超载调用驱动器
- 在使用新操作员和C 中的结构的调用构造函数时,获得内存损坏(Malloc)
- 任何操作员,功能调用和构造函数的通用持续时间表
- 如何超载操作员 如果调用对象不是类对象
- 为什么要调用复制操作员
- 是否可以使用明确的模板参数调用模板的用户定义转换操作员
- 为什么逗号运算符在运算符[]中打电话给操作员[],而不是在Operator()内部调用
- 如何在模板结构中调用操作员
- 调用指针参数从const函数指向的对象的操作员
- 您如何在unique_ptr上调用函数调用操作员
- 用两个参数调用新操作员,但没有分配
- 这些不同的方式在“ this”上调用操作员有什么区别
- 调用转换操作员
- 我可以在没有流对象的情况下调用流操作员的过载
- 在这种情况下,哪个操作员被调用
- 如何在操作员中调用操作员 =
- 为什么我的副本分配操作员从未被调用
- munmap_chunk 当操作员调用时指针无效,然后调用 destroctor
- 如何使此操作员调用明确无误