A difference? Purpose of ->*?

M

Matthew Del Buono

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
..
 
M

Matthew Del Buono

Matthew Del Buono said:
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
 
J

Jacques Labuschagne

Matthew said:
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<member_type*>::iterator i = members_.begin();
for (; i != members_.end(); ++i){
((*i)->*member_func)();
}
}
 
R

red floyd

Matthew said:
[redacted]

You misunderstand how pointer-to-member works. I suggest you go back
to your C++ text and read up on it.

#include <iostream>
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;
}
 
J

John Harrison

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top