Pointer to class member

P

pico

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::eek:bj1,10},{&B::eek:bj1,20},{&B::eek:bj3,30}};

INT B:somefunction{
int i;
for(i=0,i<3,i++){
(*(bla.objPtr)).dosomething();
}
}
 
P

Phlip

pico said:
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.
const objList bla[3]={{&B::eek:bj1,10},{&B::eek:bj1,20},{&B::eek:bj3,30}};

And those are addresses of members (really offsets of members...).
(*(bla.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.objPtr) . dosomething();

Next problem - why not simply use the Flyweight Design Pattern, so you can
get closer to bla.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.
 
V

Victor Bazarov

pico said:
[..]

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::eek:bj1,10},{&B::eek:bj1,20},{&B::eek:bj3,30}};

INT B:somefunction{
int i;
for(i=0,i<3,i++){
(*(bla.objPtr)).dosomething();


You probably meant

(bla.*objPtr).dosomething();

V
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top