Results 1 to 14 of 14

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

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

    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.
    If you want to copy/paste/move things then you should consider signal & slots for doing so, rather than looping though the entities (which will a performance issue if you have large number of entities).

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

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

    The list of entities can grow up to thousands, depends what the user draws
    I'm not sure how signal slot can improve performance, I'm not looping throw all the list entities, just throw the seleted entities.
    I'll have to think about this
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    a rather common solution would be to add a (pure) virtual method
    Qt Code:
    1. BaseClass* clone() const;
    To copy to clipboard, switch view to plain text mode 
    to BaseClass. Reimplement it in all its subclasses and just call clone() for all selected items.

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

    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.

  5. 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.