C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointer to class member

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
pico
Guest





PostPosted: Wed Jun 14, 2006 9:10 am    Post subject: Pointer to class member Reply with quote



I am attempting to write an app that allows me to reference class
members from a structure. There is only one instance of this class so
I have no problem making the members (obj1, obj2, etc.) static. I can
get build the structure fine but Im having trouble dereferencing it.
My app runs under CE so I would really like the structure to be a const
to keep it in rom. Here is a simplified version of my code. Can
someone help me out?

class A{
public:
dosoething(){return 1};
};

class B{
public:
A obj1; // these can be made static
A obj2;
A obj3;
private: // or this can be public
INT somefunction();

};

struct objList{
A B::*objPtr;
int otherdata;
}

const objList bla[3]={{&B::obj1,10},{&B::obj1,20},{&B::obj3,30}};

INT B:somefunction{
int i;
for(i=0,i<3,i++){
(*(bla[i].objPtr)).dosomething();
}
}
Back to top
Phlip
Guest





PostPosted: Wed Jun 14, 2006 9:10 am    Post subject: Re: Pointer to class member Reply with quote



pico wrote:

Quote:
I am attempting to write an app that allows me to reference class
members from a structure.

A B::*objPtr;

That is a pointer-to-member. Not just a pointer. And it should be called a
"member offset", because it's not really a pointer.

Quote:
const objList bla[3]={{&B::obj1,10},{&B::obj1,20},{&B::obj3,30}};

And those are addresses of members (really offsets of members...).

Quote:
(*(bla[i].objPtr)).dosomething();

Okay. The highest precedence is i, so start there. i indexes bla to access
objPtr, which dereferences (*) _as_a_pointer_ to access dosomething().

Member pointers are not pointers, so * doesn't work. A member pointer always
needs another variable to supply the 'this'. So if you had aB, you might be
able to do this:

(aB .* bla[i].objPtr) . dosomething();

Next problem - why not simply use the Flyweight Design Pattern, so you can
get closer to bla[i].dosomething()? That would use virtual methods for what
they do, instead of rebuilding the virtual method system from scratch.

The answer is not "because it's for CE". It's easier to make beautiful code
small than small code beautiful.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Back to top
josh
Guest





PostPosted: Wed Jan 31, 2007 10:11 am    Post subject: Re: pointer to class member Reply with quote



On 30 Gen, 16:53, Rolf Magnus <ramag...@t-online.de> wrote:
Quote:
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?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.