pointer to class member

J

josh

Hi I don't well understand if
I have

class Test
{
public:
Test(int i){ val=i;}
int double_val() { return val+val;}
int val;
};
then

Test t1(10), t2(100);

// seems static variable...............
data = &Test::val;
func = &Test::double_val;

// how can t1.*data AND t2.*data know the diferent address if data
contains
// only &Test::val ??????????
cout << "t1 data " << t1.*data << " t2 data " << t2.*data << endl;

the compiler print correctly the differents values ... but how is
possible if
I pass in data only that val address ???

Tanks
 
R

Rolf Magnus

josh said:
Hi I don't well understand if
I have

class Test
{
public:
Test(int i){ val=i;}
int double_val() { return val+val;}
int val;
};
then

Test t1(10), t2(100);

// seems static variable...............
data = &Test::val;
func = &Test::double_val;

// how can t1.*data AND t2.*data know the diferent address if data
contains
// only &Test::val ??????????

Because you're explicitly specifying it.
cout << "t1 data " << t1.*data << " t2 data " << t2.*data << endl;

the compiler print correctly the differents values ... but how is
possible if I pass in data only that val address ???

When accessing it, you're not writing just *data, but t1.*data. So it's easy
for the compiler to know that you want t1.val and not t2.val.
 
J

josh

Ok but when should I use this methodology?
provides only an offset into an object of the member's class at which
that member can
be found.

so which is the difference between an offset and a pointer???????
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Ok but when should I use this methodology?

Rarely as far as I'm concerned.I'm not saying that there are no
situations where this is not useful, but for most occasions there are
other ways to solve the problem.
provides only an offset into an object of the member's class at which
that member can be found.

so which is the difference between an offset and a pointer???????

A pointer is the absolute address to something, the number of bytes
from the beginning of the address-space to the thing. An offset is
relative, the number of bytes from the beginning of something to some
other thing. A pointer to a member is an offset from the start of the
instance of a class to the member.

So to get the address of the member you could take the address of the
class and add the offset in the member-pointer.
 
V

Victor Bazarov

josh said:
Ok but when should I use this methodology?

provides only an offset into an object of the member's class at which
that member can
be found.

so which is the difference between an offset and a pointer???????

Whatever the author meant by "offset" is not actually just an offset
(which is usually a relatively small integral value). Pointers to
members are more complicated, calling them "only an offset" is trying
to make it simpler to understand that in order to use it you still
need an instance of the class.

V
 
R

Rolf Magnus

josh said:
Ok but when should I use this methodology?

Well, I have never needed to use a pointer to data member, and actually
don't know what I would need it for, but pointers to member functions on
the other hand can be useful e.g. for callback functions.
 
P

peter koch

Well, I have never needed to use a pointer to data member, and actually
don't know what I would need it for, but pointers to member functions on
the other hand can be useful e.g. for callback functions.

Pointers to datamembers can be useful when creating intrusive
structures. I have used that technique to create e.g. intrusive lists
(which IMHO are useful far more often than std::list). But I admit
they are rare beasts.

/Peter
 
J

josh

Well, I have never needed to use a pointer to data member, and actually
don't know what I would need it for, but pointers to member functions on
the other hand can be useful e.g. for callback functions.

can you make, please, a concrete example?
 
R

Rolf Magnus

josh said:
can you make, please, a concrete example?

You can use them e.g. with the standard algorithms, something like:

std::vector<GraphicObject*> vec;
std::for_each(vec.begin(), vec.end(), std::mem_fun(&GraphicObject::draw));
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top