pointer to member's member?

G

Georg D.

hello,

with

struct C
{
string f1;
};

class A
{
C data;
};

what is the best way to pass member pointer to f1
relative to class A, sth. like "&A::data.f1" ?

---- backgroud:
i'd like to compose classes in following way:

--- generic part ---

// carry some data
class field
{ ...
};

// keep information on (contained) fields
class record_definition
{ ...
};

// and perform common actions
class record
{
shared_ptr<record_definition> recdef;
public:
record(shared_ptr<record_definition> _recdef) : recdef(_recdef) ...
...
};


---- user's part ----

// here is the data struct
struct data_t
{
field f1;
field f2;
...
};


// here, the data_t is enhanced with
// functionality from record
// my_record provides record definition
// so class record has acces to the fields
class my_record : public record
{
protected: // but could be public as well
data_t data;
};

----
my_record would pass pointers to member's member fields
f1, f2 etc.

TIA,
georg
 
B

Buster

Georg said:
with

struct C
{
string f1;
};

class A
{
C data;
};

what is the best way to pass member pointer to f1
relative to class A, sth. like "&A::data.f1" ?

A pointer-to-member is just that. You can't make one point to a member
of a member. You have to simulate that with two pointers-to-member.

struct string { };

struct C
{
string f1;
};

class A
{
public:
C data;
};

struct pmm
{
pmm (C A::* p, string C::* q) : p (p), q (q) { }
string & dereference_on (A & a) { return a.* p.* q; }
private:
C A::* p;
string C::* q;
};

int main ()
{
A a;
pmm p (& A::data, & C::f1);

string x = p.dereference_on (a);
// You can overload operators to get "x = a * p", or "x = p (a)", or
// something else, but you cannot get "x = a.* p".
}
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top