What does the operator do. * ()?

What does it do operator.*()? What is the purpose?

It is documented as a Pointer-to-member and is exactly the same as ->*. Are these two identical?

Scott Meyers in more efficient C ++, clause 7 explains that it is .*NOT overloaded, but ->*IS. Why is this?

+4
source share
4 answers

As already mentioned, no, .*and ->*do not mean the same thing. Assuming that overloaded operators are not used, a->*bmeans (*a).*b, i.e. .*used with class types, and ->*used with pointers to class types. This is the same as a->bmeaning (*a).bif built-in operators are used.

., .* , , , . . , ++, , 1990 ( , ++). , , , , a.b , () (a.operator.()).b. , ? .

. .*, - . .* , , - , , , ., .

+3

:

" "

#include <iostream>
#include <string>
using std::string;

class Foo{
public:
  int f(string str){
    std::cout<<"Foo::f()"<<std::endl;
    return 1;
  }
};

int main(int argc, char* argv[]){
  int (Foo::*fptr) (string) = &Foo::f;
  Foo obj;
  (obj.*fptr)("str");//call: Foo::f() through an object
  Foo* p=&obj;
  (p->*fptr)("str");//call: Foo::f() through a pointer
}

, , , , , ,

, . → , ,

, , x- > (* x)., .() , *() , , → , " "

, , - , ++ .

+2

- . :

// This functions waits until predicate evalues false
void waitSomething(bool (one_class::*predicate)(),one_class& that)
{
    while ((that.*predicate)())
    {
        sleep(100);
    }
}

:

one_class a;
waitSomething(&a::*predicate,a); //predicate is an internal function member

.

- > * :

// This functions waits until predicate evalues false
void waitSomething(bool (one_class::*predicate)(),one_class* that)
{
    while ((that->*predicate)())
    {
        sleep(100);
    }
}
0

, , , .

->* ( ...) -> ( std::unique_ptr) . , , std::unique_ptr ->, ->*.

., .*, , .

So, here is a somewhat stupid class that wraps an object and pretends to be a pointer to it. The example makes sense if the managed object is held by a pointer with a controlled lifetime, but I would like to avoid cluttering the example with resource management code that is not related to member access.

// C++14

#include <iostream>
#include <utility>
#include <vector>

template<typename T>
class PointerLike
{

private:

  T object_;

public:

  template<typename... ParamT>
  PointerLike(ParamT&&... params) : object_ {std::forward<ParamT>(params)...}
  {
  }

  // The 'const' overloads have been omitted for the sake of brevity.

  // "Dereferences" the handle and obtains a reference to the managed object.
  T&
  operator*()
  {
    return this->object_;
  }

  // Accesses a member of the managed object.
  T *
  operator->()
  {
    return &(this->object_);
  }

  // Indirectly invokes a function member of the managed object.
  template<typename RetT, typename... ArgT>
  decltype(auto)
  operator->*(RetT(T::*mfunc)(ArgT...))
  {
    return [=](ArgT... args)->RetT{
      return (object_.*mfunc)(std::forward<ArgT>(args)...);
    };
  }
};

int
main()
{
  typedef std::vector<int> ivec;
  typedef void (ivec::*append_member_type)(int&&);
  append_member_type append = &ivec::push_back;
  PointerLike<ivec> pl {1, 2, 3, 4};
  pl->push_back(5);    // pointer-like direct member access
  (pl->*append)(6);    // pointer-like indirect member access ("natural" way)
  ((*pl).*append)(7);  // pointer-like indirect member access
  for (const auto& v : *pl)
    std::cout << v << std::endl;
}

The program will output integers from 1 to 7.

-1
source

All Articles