small question

I am just wondering what these operators do? I can't find the answer on google :<
I know they are member access operators, but I can't understand what they do.

 
   ->*    .*
That is not what I am looking for...
a->*b is means the member pointed to by b of object pointed to by a

a.*b is means the member pointed to by b of object a
Last edited on
Any example code, please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

class Testpm
{
public:
    int m_num;
};

int Testpm::*pmd = &Testpm::m_num;

int main()
{
    Testpm ATestpm;
    Testpm *pTestpm = new Testpm;
    
    ATestpm.*pmd = 1;
    pTestpm->*pmd = 2;
    
    cout  << ATestpm.*pmd << endl
    << pTestpm->*pmd << endl;
    
    delete pTestpm;
}
Not a very good example but what the hell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}


They get more interesting when the code gets more complicated.
Last edited on
@Grey Wolf :
i just passed by and saw the code you posted.
can you please explain this line:
int Testpm::*pmd = &Testpm::m_num;

pmd is not a member of Testpm, how did you define it?
i'm really unfamiliar with this kind of declaration.

also:
int (Foo :: * ptr);

are you actually declaring new members outside of the struct declaration?
int *p; a pointer to integer
int Testpm::*pmd; a pointer to integer that it is a member of `Testpm' class
int (Foo :: * ptr); superfluous parenthesis.

while we are at it
void (*foo)(int n); a function pointer, to functions that return nothing and receive an integer parameter
void (Foo::*foo) (int n);a member function pointer, to member functions of class `Foo' that return nothing and receive an integer parameter
void (*foo)(Foo *this, int n); "equivalent" C form (conceptual)

1
2
3
4
5
6
7
8
9
10
class Foo{
public:
   void bar(int n);
};

int main(){
   Foo asdf;
   void (Foo::*ptr)(int n) = &Foo::bar;
   (asdf.*ptr)(42); //note the parenthesis, because of precedence rules
}
Last edited on
Topic archived. No new replies allowed.