446,193 Members | 826 Online
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

# A difference? Purpose of ->*?

 P: n/a AFAIK... Order of precedence of operators: post ++ post -- .. .. .. -> .. pre ++ .. .. .. * (indirection) sizeof new delete (type) (typecast) ..* ->* .. .. |= , So what exactly is the point of .* and ->* (other than giving maybe a neater or other way of writing things)? Aren't these the same? (Assume class A is defined with a pointer to an integer "bar"): A foo; A* pFoo = new A; foo.bar = 4; *foo.bar = 4; pFoo->*bar = 4; *pFoo->bar = 4; As far as I can tell, all of these assignments perform the same task. Now, I'm sure it is much easier to read pFoo->*bar than *pFoo->bar (it appears in the latter example that pFoo is trying to be dereferenced). Is that the only reason for the operator? -- Matt .. Jul 22 '05 #1
4 Replies

 P: n/a "Matthew Del Buono" wrote in message news:2Eepc.25006\$Lm3.5077@lakeread04... AFAIK... Order of precedence of operators: post ++ post -- . . . -> . pre ++ . . . * (indirection) sizeof new delete (type) (typecast) .* ->* . . |= , So what exactly is the point of .* and ->* (other than giving maybe a neater or other way of writing things)? Aren't these the same? (Assume class A is defined with a pointer to an integer "bar"): A foo; A* pFoo = new A; foo.bar = 4; *foo.bar = 4; pFoo->*bar = 4; *pFoo->bar = 4; As far as I can tell, all of these assignments perform the same task. Now, I'm sure it is much easier to read pFoo->*bar than *pFoo->bar (it appears in the latter example that pFoo is trying to be dereferenced). Is that the only reason for the operator? -- Matt . Actually as I'm writing this program, I have found one instance in which the *foo->bar method would be required: class foo { public: int* bar(); } .... *foo->bar() = 4; // legal -- set to 4 at address returned by bar() foo->*bar() = 4; // not legal -- cannot dereference a function // (not an address) Am I right? (I didn't try to compile this example) -- Matt Jul 22 '05 #2

 P: n/a Matthew Del Buono wrote: So what exactly is the point of .* and ->* (other than giving maybe a neater or other way of writing things)? Aren't these the same? (Assume class A is defined with a pointer to an integer "bar"): What if you're trying to apply a pointer-to-member function to a pointer to an object? void my_class::apply_to_each_element(void (member_type::*member_func)()) { vector::iterator i = members_.begin(); for (; i != members_.end(); ++i){ ((*i)->*member_func)(); } } Jul 22 '05 #3

 P: n/a Matthew Del Buono wrote: [redacted] You misunderstand how pointer-to-member works. I suggest you go back to your C++ text and read up on it. #include using namespace std; class A { public: int foo; int bar; }; int main(int, char *[]) { int A::*member; A anA; anA.foo = 2; anA.bar = 3; member = &A::foo; cout << anA.*member << endl; // prints 2; member = &A::bar; anA.*member = 7; cout << anA.bar << endl; // prints 7; return 0; } Jul 22 '05 #4

 P: n/a Am I right? (I didn't try to compile this example) If you had tried you would have found that you have completely the wrong idea. Read up on pointers to member functions, and pointers to members (this last one is really obscure, many books won't cover it at all, and I have never seen a meaningful program that uses them). john Jul 22 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.