Please comment on the code

Discussion in 'C++' started by sci, Apr 13, 2004.

  1. sci

    sci Guest

    For B to call back a member of A, the code is design as:

    class A;

    class B
    {
    A* p;
    public:
    void Init(void* m) { p = (A*)m; }
    void docallback();
    };


    class A
    {
    B b;
    public:
    A() { b.Init(this); }
    void dowork() { }

    };


    void B::docallback()
    {
    ((A*)p)->dowork();
    }

    Thanks!
     
    sci, Apr 13, 2004
    #1
    1. Advertising

  2. sci wrote:
    > For B to call back a member of A, the code is design as:
    >
    > class A;
    >
    > class B
    > {
    > A* p;
    > public:
    > void Init(void* m) { p = (A*)m; }


    C-style casts are evil. Why didn't you just make Init take a A*?

    > void docallback();
    > };
    >
    >
    > class A
    > {
    > B b;
    > public:
    > A() { b.Init(this); }
    > void dowork() { }
    >
    > };
    >
    >
    > void B::docallback()
    > {
    > ((A*)p)->dowork();


    Why are you casting p to the type it already is?

    > }
    >


    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 13, 2004
    #2
    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. Analog_Guy
    Replies:
    6
    Views:
    577
    Andy Peters
    Jun 1, 2005
  2. Alec S.
    Replies:
    10
    Views:
    10,268
    Alec S.
    Apr 16, 2005
  3. Replies:
    7
    Views:
    332
  4. Replies:
    58
    Views:
    1,047
    Ben Pope
    Mar 15, 2006
  5. Damjan Rems
    Replies:
    0
    Views:
    100
    Damjan Rems
    Mar 16, 2007
Loading...

Share This Page