473,394 Members | 2,160 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,394 software developers and data experts.

Pointer to class member

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

Jun 14 '06 #1
2 1556
pico wrote:
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::obj1,10},{&B::obj1,20},{&B::obj3,30}};
And those are addresses of members (really offsets of members...).
(*(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!!!
Jun 14 '06 #2
pico wrote:
[..]

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();
You probably meant

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


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 14 '06 #3

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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.