Results 1 to 2 of 2

Thread: Casting a CRect object/pointer to a CObject pointer

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Glasgow
    Posts
    47

    Casting a CRect object/pointer to a CObject pointer

    I am trying to use the following line of code to cast a CRect to a CCbject:


    CObject* pHint=DYNAMIC_DOWNCAST(CObject, mRects[x][y]);

    but I get an error stating "none of the two overloads can convert parameter 2 from type 'Class CRect*'"

    I need this to use in CView OnUpdate so that I only redraw part of the screen.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    CRect is not derived from CObject.
    But you can use brute pointer casting:
    UpdateAllViews(NULL, 0L, reinterpret_cast<CObject*>(&rcUpdate));

    Just remember to cast it back in OnUpdate.

    A tip: if you're ever going to use a selfmade struct UPDATE_PARAMS (or something like that), you may want to add a CObject* operator to the struct:
    Code:
    struct UpdateParams
    {
      int type;
      CRect rcUpdate;
      long id;
      operator CObject*() { return reinterpret_cast<CObject*>this; };
    };
    
    // somewhere:
    UpdateParams up;
    // set members
    UpdateAllViews(this, 0L, up);
    This makes it easier to write and read, and since you don't use UpdateParams anywhere else (hopefully), it isn't a type safety hazard IMO.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width