pointer to member's member?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Georg D.

    pointer to member's member?

    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.f 1" ?

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

    --- generic part ---

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

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

    // and perform common actions
    class record
    {
    shared_ptr<reco rd_definition> recdef;
    public:
    record(shared_p tr<record_defin ition> _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




  • Buster

    #2
    Re: pointer to member's member?

    Georg D. wrote:
    [color=blue]
    > 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.f 1" ?[/color]

    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_o n (a);
    // You can overload operators to get "x = a * p", or "x = p (a)", or
    // something else, but you cannot get "x = a.* p".
    }

    --
    Regards,
    Buster.

    Comment

    Working...