C++ Design question

Discussion in 'C++' started by denis, Jan 14, 2007.

  1. denis

    denis Guest

    Hi All,

    I'm making a class which derives(implements) from 5 interfaces:

    class myClass: public base1, public base2, public base3,.......

    I want to be able to ask from any interface about a pointer to another
    interface
    What I mean is:

    myClass _myImpObj;

    base1 *b1 = &_myImpObj;
    base *b2 = b1->QueryInterafce("b2");
    base *b3 = b2->QueryInterafce("b3");
    base *b1N = b3->QueryInterface("b1");

    Its is very simialir to COM mechanism.Anyone knows how to implement it?

    Thanks,
    Denis
     
    denis, Jan 14, 2007
    #1
    1. Advertising

  2. denis

    alon Guest

    void* myClass::QueryInterface(char* str)
    {
    if (!strcmp(str, "b1"))
    return (base1*)this;
    ....
    ....
    }

    Is this what you meant?


    denis wrote:
    > Hi All,
    >
    > I'm making a class which derives(implements) from 5 interfaces:
    >
    > class myClass: public base1, public base2, public base3,.......
    >
    > I want to be able to ask from any interface about a pointer to another
    > interface
    > What I mean is:
    >
    > myClass _myImpObj;
    >
    > base1 *b1 = &_myImpObj;
    > base *b2 = b1->QueryInterafce("b2");
    > base *b3 = b2->QueryInterafce("b3");
    > base *b1N = b3->QueryInterface("b1");
    >
    > Its is very simialir to COM mechanism.Anyone knows how to implement it?
    >
    > Thanks,
    > Denis
     
    alon, Jan 14, 2007
    #2
    1. Advertising

  3. denis

    kwikius Guest

    denis wrote:
    > Hi All,
    >
    > I'm making a class which derives(implements) from 5 interfaces:
    >
    > class myClass: public base1, public base2, public base3,.......
    >
    > I want to be able to ask from any interface about a pointer to another
    > interface
    > What I mean is:
    >
    > myClass _myImpObj;
    >
    > base1 *b1 = &_myImpObj;
    > base *b2 = b1->QueryInterafce("b2");
    > base *b3 = b2->QueryInterafce("b3");
    > base *b1N = b3->QueryInterface("b1");
    >
    > Its is very simialir to COM mechanism.Anyone knows how to implement it?


    You can use dynamic_cast:

    #include <iostream>

    template <int N>
    struct base{
    virtual ~base(){}
    };

    struct my12 : base<1>, base<2>{};

    struct my34 : base<3>, base<4>{};

    struct my5 : base<5>{};

    struct my12345 : base<1>,base<2>, my34 ,my5{};

    template <int N, typename T>
    bool has_interface(T * t)
    {
    bool is = dynamic_cast< base<N> * > (t) !=0;
    std::cout << typeid(T).name() << " "
    << ((is)?"Is":"Is not")<< " a base<" << N << ">\n";
    return is;
    }

    template <int N>
    struct interfaces;

    template <>
    struct interfaces<1>{
    template <typename T>
    void operator()( T * t)const
    {
    has_interface<1>(t);
    }
    };

    template <int N>
    struct interfaces{
    template <typename T>
    void operator()( T * t)const
    {
    interfaces<N-1> f;
    f(t);
    has_interface<N>(t);
    }
    };

    int main()
    {
    //test 5 interfaces
    interfaces<5> test;

    //test some expressions
    my12 var12;
    test(&var12);

    std::cout <<"---------\n\n";

    my34 var34;
    test(&var34);

    std::cout <<"---------\n\n";

    my5 var5;
    test(&var5);

    std::cout <<"---------\n\n";

    my12345 var12345;
    test(&var12345);

    }





    regards
    Andy Little
     
    kwikius, Jan 14, 2007
    #3
  4. denis

    kwikius Guest

    kwikius wrote:

    //forgot crosscast test:

    /*
    ....
    */

    > std::cout <<"---------\n\n";
    >
    > my12345 var12345;
    > test(&var12345);



    std::cout <<"---------\n\n";

    //crosscast test
    base<1>* bb = &var12345;

    test(bb);

    >
    > }


    regards
    Andy Little
     
    kwikius, Jan 14, 2007
    #4
  5. denis

    Jorgen Grahn Guest

    On 14 Jan 2007 01:42:35 -0800, denis <> wrote:
    > Hi All,
    >
    > I'm making a class which derives(implements) from 5 interfaces:
    >
    > class myClass: public base1, public base2, public base3,.......
    >
    > I want to be able to ask from any interface about a pointer to another
    > interface
    > What I mean is:
    >
    > myClass _myImpObj;
    >
    > base1 *b1 = &_myImpObj;
    > base *b2 = b1->QueryInterafce("b2");
    > base *b3 = b2->QueryInterafce("b3");
    > base *b1N = b3->QueryInterface("b1");
    >
    > Its is very simialir to COM mechanism.Anyone knows how to implement it?


    Not knowing what you want to accomplish, this sounds like a really bad idea.
    Both the code itself, and the wish to emulate COM.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Jan 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ZackS
    Replies:
    5
    Views:
    6,871
    Just an Illusion
    Jul 9, 2004
  2. SpamProof
    Replies:
    3
    Views:
    671
    SpamProof
    Dec 1, 2003
  3. dave
    Replies:
    5
    Views:
    618
    William Brogden
    Jul 17, 2004
  4. Tim Smith
    Replies:
    2
    Views:
    885
    Tim Smith
    Dec 15, 2004
  5. Bartholomew Simpson

    class design/ design pattern question

    Bartholomew Simpson, Jun 12, 2007, in forum: C++
    Replies:
    2
    Views:
    465
    Daniel T.
    Jun 12, 2007
Loading...

Share This Page