473,412 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

A difference? Purpose of ->*?

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 1189

"Matthew Del Buono" <Ma******@nospam.com> 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
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<member_type*>::iterator i = members_.begin();
for (; i != members_.end(); ++i){
((*i)->*member_func)();
}
}

Jul 22 '05 #3
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 <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;
}

Jul 22 '05 #4

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ryan Lange | last post by:
Ok, I'm looking at a function whose purpose is to "prepare" the contents (usually input by a user) of a variable to be inserted into a MySQL database. It's pretty simple, really; just check to see...
1
by: G. Smith Q news | last post by:
What is the difference between an abstract class and an Interface. As a novice, they both seem to serve the same purpose?
3
by: Glen Scott | last post by:
Hi, I'm writing an ASP app that administers an ISA server remotely. The fact that it's an ISA server isn't my problem I believe. My question? What is the security difference between disabling...
2
by: Ma Xiaoming | last post by:
Dear ladies and gentlemen, I don't understand what the difference between the Heap and the Stack is. Could you please explain the difference between the both for me? Thank you very much. ...
5
by: rodchar | last post by:
hey all, what's the difference between span and div tags? thanks, rodchar
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
1
by: bharathreddy | last post by:
This Article gives an introduction to VSTS Team Foundation & fundamental difference between Visual Source Safe (VSS) and VSTS Team Foundation. Team Foundation is a set of tools and technologies...
3
by: Frank Rizzo | last post by:
In the WinForms context.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.