Results 1 to 3 of 3

Thread: dynamic_cast not working

  1. #1
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default dynamic_cast not working

    Do I need to recompile qmake with the exceptions, rtti and stl flags to get dynamic_cast to work with QT? I'm using Visual C++ 2005 and the /GR flag is enabled (by default).

    I'm asking because have implemented a simple Composite Pattern and I need to find out whether the Component is a Leaf or a Composite. This is what I would like to do:
    Qt Code:
    1. try
    2. {
    3. Leaf* leaf = dynamic_cast<Leaf*>(component);
    4. //A Leaf
    5. }
    6. catch (std::bad_cast e)
    7. {
    8. //Not a Leaf
    9. }
    To copy to clipboard, switch view to plain text mode 
    However, in practice the exception is never thrown and the program dies because of memory access violations.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamic_cast not working

    If the cast fails then leaf will be NULL. No need to catch any exceptions. Just test the leaf.

  3. The following user says thank you to marcel for this useful post:

    kloffy (12th October 2007)

  4. #3
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: dynamic_cast not working

    Ahhh, I should really learn to read...
    Quote Originally Posted by [URL=http://msdn2.microsoft.com/en-us/library/ms861515.aspx]MSDN[/URL]
    The value of a failed cast to pointer type is the null pointer. A failed cast to reference type throws a bad_cast Exception.
    Thanks for the heads up!

Similar Threads

  1. QTableView doubleclicked event not working
    By sgmurphy19 in forum Qt Programming
    Replies: 6
    Last Post: 15th March 2009, 11:00
  2. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 16:31
  3. Connect not working
    By xgoan in forum Qt Programming
    Replies: 4
    Last Post: 24th July 2006, 12:27
  4. Replies: 1
    Last Post: 11th June 2006, 23:25
  5. QSettings - beginReadArray not working
    By Mike in forum Qt Programming
    Replies: 7
    Last Post: 9th January 2006, 22:24

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.