IT Share you

C ++에서 Pointer-to-Member-> * 및. * 연산자는 무엇입니까?

shareyou 2020. 12. 15. 20:25
반응형

C ++에서 Pointer-to-Member-> * 및. * 연산자는 무엇입니까?


그래, 난 본 적이 이 질문 하고 이 FAQ (잘못된 링크) 이 FAQ를 ,하지만 난 아직도 이해하지 않는 C ++로 의미한다. 이러한 페이지 는 연산자 대한 정보 (예 : 오버로딩)를 제공하지만 연산자 무엇인지 잘 설명하지 않는 것 같습니다 . ->*.*

무엇 ->*.*C ++에서, 그리고 때와 비교를 사용해야합니까 ->.?


이 예제가 당신을 위해 일을 분명히 해주길 바랍니다.

//we have a class
struct X
{
   void f() {}
   void g() {}
};

typedef void (X::*pointer)();
//ok, let's take a pointer and assign f to it.
pointer somePointer = &X::f;
//now I want to call somePointer. But for that, I need an object
X x;
//now I call the member function on x like this
(x.*somePointer)(); //will call x.f()
//now, suppose x is not an object but a pointer to object
X* px = new X;
//I want to call the memfun pointer on px. I use ->*
(px ->* somePointer)(); //will call px->f();

지금, 당신은 사용할 수 없습니다 x.somePointer(), 또는 px->somePointer()특별한 멤버 함수 포인터 호출 구문을 사용하는 것을 들어 클래스 X에서 그런 멤버가 없기 때문에 ... 몇 가지 예를 직접하려고하면 그것에 익숙해 것


편집 : 그건 그렇고, 가상 멤버 함수 포인터에 대해 이상해집니다 .

멤버 변수의 경우 :

struct Foo {
   int a;
   int b;
};


int main ()
{
    Foo foo;
    int (Foo :: * ptr);

    ptr = & Foo :: a;
    foo .*ptr = 123; // foo.a = 123;

    ptr = & Foo :: b;
    foo .*ptr = 234; // foo.b = 234;
}

멤버 기능은 거의 동일합니다.

struct Foo {
   int a ();
   int b ();
};


int main ()
{
    Foo foo;
    int (Foo :: * ptr) ();

    ptr = & Foo :: a;
    (foo .*ptr) (); // foo.a ();

    ptr = & Foo :: b;
    (foo .*ptr) (); // foo.b ();
}

요컨대 : 당신은 당신이 사용 ->하고 .당신이 액세스하려는 회원을 알고 있다면. 그리고 당신은 사용 ->*하고 .*있다면 당신은 하지 않는 액세스를 원하는 회원 알고있다.

간단한 침입 목록이있는 예

template<typename ItemType>
struct List {
  List(ItemType *head, ItemType * ItemType::*nextMemPointer)
  :m_head(head), m_nextMemPointer(nextMemPointer) { }

  void addHead(ItemType *item) {
    (item ->* m_nextMemPointer) = m_head;
    m_head = item;
  }

private:
  ItemType *m_head;

  // this stores the member pointer denoting the 
  // "next" pointer of an item
  ItemType * ItemType::*m_nextMemPointer;
};

So called "pointers" to members in C++ are more like offsets, internally. You need both such a member "pointer", and an object, to reference the member in the object. But member "pointers" are used with pointer syntax, hence the name.

There are two ways you can have an object at hand: you have a reference to the object, or you have a pointer to the object.

For the reference, use .* to combine it with a member pointer, and for the pointer, use ->* to combine it with a member pointer.

However, as a rule, don't use member pointers if you can avoid it.

They obey pretty counter-intuitive rules, and they make it possible to circumvent protected access without any explicit casting, that is, inadvertently…

Cheers & hth.,


You cannot dereference pointer to members as normal pointers — because member functions require this pointer, and you have to pass it somehow. So, you need to use these two operators, with object on one side, and pointer on another, e.g. (object.*ptr)().

Consider using function and bind (std:: or boost::, depending on whether you write C++03 or 0x) instead of those, though.


When you have a normal pointer (to an object or a basic type), you would use * to dereference it:

int a;
int* b = a;
*b = 5;     // we use *b to dereference b, to access the thing it points to

Conceptually, we're doing the same thing with a member function pointer:

class SomeClass
{
   public:  void func() {}
};

// typedefs make function pointers much easier.
// this is a pointer to a member function of SomeClass, which takes no parameters and returns void
typedef void (SomeClass::*memfunc)();

memfunc myPointer = &SomeClass::func;

SomeClass foo;

// to call func(), we could do:
foo.func();

// to call func() using our pointer, we need to dereference the pointer:
foo.*myPointer();
// this is conceptually just:    foo  .  *myPointer  ();


// likewise with a pointer to the object itself:
SomeClass* p = new SomeClass;

// normal call func()
p->func();

// calling func() by dereferencing our pointer:
p->*myPointer();
// this is conceptually just:    p  ->  *myPointer  ();

I hope that helps explain the concept. We're effectively dereferencing our pointer to the member function. It's a little more complicated than that -- it's not an absolute pointer to a function in memory, but just an offset, which is applied to foo or p above. But conceptually, we're dereferencing it, much like we would dereference a normal object pointer.

ReferenceURL : https://stackoverflow.com/questions/6586205/what-are-the-pointer-to-member-and-operators-in-c

반응형