Results 1 to 14 of 14

Thread: Can I cast a pointer from a unknow type ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Can I cast a pointer from a unknow type ?

    @OP:
    You have simplest case of what inheritance is there to solve, you are over complicating it big time (probably because you don't grasp yet how and what inheritance does).

    This is exactly what I want, I'll explain: this is a cad projetc where the user can draw entities (lines, polylines, ...). If the user draws 3 lines and 2 polylines (by this order), m_listEntities will have 5 elements. If the user selects some entities (lets say 1 line and 1 polyline) the indexes of the select entities are stored in QList m_listEntities_selected (wich is if of type int, and it will have 2 elements, the indexes of the selected entities).
    The user can create a copy of the selected entities and move then with the mouse to a new position. The cicle for is looping throw all the select entities, copies them and add them to m_listEntities wich now will have 7 elements.
    This can (and should) be solved using OOP.
    You don't need an extra list for the selected entities, all you need is a setSelected() and isSelected() methods in your base entity class.
    Using a list, or better yet a set of the selected entities so that you can save the looping when you search you selected items - then use a set that stores the pointers of the selected entities.
    But information it self, if an item is selected or not should be encapsulated in to the object, not in an external list!

    So:
    Instead of you m_listEntities_selected make a:
    Qt Code:
    1. QSet<BaseClass*> m_setSelectedEnteties;
    To copy to clipboard, switch view to plain text mode 
    There are many ways you can fill this set, one easy way would be that your entities emit a signal when they are selected, and in the slot you just add the address of the sender to the set.
    You do the same for removing the items from the set in a slot connected to a "unselected" signal.
    Now that you have your selected entities, you can do as the poster above me suggested:
    Qt Code:
    1. foreach (BaseClass *pSelectedEntity, m_setSelectedEnteties){
    2. BaseClass *pEntity = pSelectedEntity->clone();
    3. m_listEntities->append(pEntity);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Nice and easy.
    As you can see, thanks to correct usage of inheritance, you don't have to bother with types.
    The specific implementation for each type is encapsulated in the implementation of the virtual method in the various BaseClass subclasses.
    Last edited by high_flyer; 30th May 2011 at 13:05.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. The following user says thank you to high_flyer for this useful post:

    john_god (30th May 2011)

Similar Threads

  1. Replies: 1
    Last Post: 4th December 2010, 17:20
  2. Pointer of an unknown type...
    By TheNewGuy in forum Newbie
    Replies: 1
    Last Post: 17th December 2009, 08:50
  3. Dynamic pointer type
    By estanisgeyer in forum General Programming
    Replies: 3
    Last Post: 9th October 2008, 16:51
  4. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58
  5. cast
    By mickey in forum General Programming
    Replies: 1
    Last Post: 12th July 2006, 11:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.