473,387 Members | 1,540 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,387 software developers and data experts.

pointer to class member

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

Jan 30 '07 #1
8 1575
josh wrote:
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.

Jan 30 '07 #2
Ok but when should I use this methodology?

and in a book is wrote:
>>a pointer to a member
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???????

Jan 30 '07 #3
On Jan 30, 3:42 pm, "josh" <xdevel2...@yahoo.comwrote:
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.
and in a book is wrote:
>a pointer to a member

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.

--
Erik Wikström

Jan 30 '07 #4
josh wrote:
Ok but when should I use this methodology?

and in a book is wrote:
>>>a pointer to a member
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 30 '07 #5
josh wrote:
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.

Jan 30 '07 #6


On Jan 30, 4:53 pm, Rolf Magnus <ramag...@t-online.dewrote:
josh wrote:
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.
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

Jan 30 '07 #7
On 30 Gen, 16:53, Rolf Magnus <ramag...@t-online.dewrote:
josh wrote:
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.
can you make, please, a concrete example?

Jan 31 '07 #8
josh wrote:
On 30 Gen, 16:53, Rolf Magnus <ramag...@t-online.dewrote:
>josh wrote:
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.

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));

Jan 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
31
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.