Results 1 to 11 of 11

Thread: Is the down casting right, and why dynamic_cast fails?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    9

    Default Is the down casting right, and why dynamic_cast fails?

    Hello guys,
    here I have a snippet of code and it would be very helpful, if you can explain why the second dynamic_cast fails and if the static_cast is the way to do it.

    There are three classes Base, MyString and MyList. MyString and MyList are derived from Base. MyList is a template-class
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. class Base
    5. {
    6. public:
    7. Base(){
    8.  
    9. }
    10.  
    11. virtual ~Base(){
    12. qDebug() << "~Base()";
    13. }
    14. };
    15.  
    16. class MyString : public Base
    17. {
    18. public:
    19. QString str;
    20.  
    21. public:
    22. MyString(const QString& str) : Base(), str(str){
    23.  
    24. }
    25.  
    26. virtual ~MyString(){
    27. qDebug() << "~MyString()";
    28. }
    29. };
    30.  
    31. template <class T>
    32. class MyList : public QList<T>, public Base
    33. {
    34. public:
    35. MyList<T>():QList<T>(){
    36.  
    37. }
    38.  
    39. MyList<T>(QList<T> h):QList<T>(h){
    40.  
    41. }
    42.  
    43. virtual ~MyList<T>(){
    44. qDebug() << "~MyList()";
    45. }
    46. };
    47.  
    48. Base* createMyList()
    49. {
    50. MyList<Base*>* myList = new MyList<Base*>();
    51. myList->append(new MyString("joey potter"));
    52. myList->append(new MyString("allen harper"));
    53. myList->append(new MyString("charlie harper"));
    54. myList->append(new MyString("john mcclane"));
    55.  
    56. return myList;
    57. }
    58.  
    59. int main(int argc, char *argv[])
    60. {
    61. QCoreApplication a(argc, argv);
    62. Q_UNUSED(a);
    63.  
    64. MyList<Base*>* myList = new MyList<Base*>();
    65. myList->append(new MyString("string1"));
    66.  
    67. //I know that first element is from class MyString so static_cast is sufficient
    68. Q_ASSERT(dynamic_cast<MyString*>(myList->first()) != NULL);// ok
    69. Q_ASSERT(static_cast<MyString*>(myList->first()) != NULL);// ok
    70.  
    71. MyList<MyString*>* anotherList = static_cast<MyList<MyString*>*>(createMyList());
    72. Q_ASSERT(anotherList != NULL);// ok
    73.  
    74. MyList<MyString*>* differentList = dynamic_cast<MyList<MyString*>*>(createMyList());
    75. Q_ASSERT(differentList != NULL);// ASSERT: "differentList != NULL" in file ../dynamic_cast_test/main.cpp, line 74
    76.  
    77.  
    78.  
    79.  
    80.  
    81.  
    82.  
    83. //free ram
    84. QListIterator<Base*> lit1(*myList);
    85. while(lit1.hasNext()){
    86. delete lit1.next();
    87. }
    88. delete myList;
    89.  
    90. QListIterator<MyString*> lit2(*anotherList);
    91. while(lit2.hasNext()){
    92. delete lit2.next();
    93. }
    94. delete anotherList;
    95.  
    96. QListIterator<MyString*> lit3(*differentList);
    97. while(lit3.hasNext()){
    98. delete lit3.next();
    99. }
    100. delete differentList;
    101.  
    102. return 0;
    103. }
    To copy to clipboard, switch view to plain text mode 

    Why does following cast fail?
    Qt Code:
    1. MyList<MyString*>* differentList = dynamic_cast<MyList<MyString*>*>(createMyList())
    To copy to clipboard, switch view to plain text mode 
    Cause MyList<Base*>* is completely different from MyList<MyString*>* and the dynamic check is technically impossible ?

    But can I do a static_cast(no dynamic checks), cause I know it's a MyList<MyString*>*? Is this cast is safe?

    thank you
    Last edited by QtCrawler; 8th December 2015 at 12:10.

Similar Threads

  1. help dynamic_cast
    By giorgik in forum Qt Programming
    Replies: 12
    Last Post: 5th October 2012, 09:29
  2. qobject_cast vs dynamic_cast
    By mike_c in forum Qt Programming
    Replies: 5
    Last Post: 19th April 2010, 19:51
  3. dynamic_cast not working
    By kloffy in forum Newbie
    Replies: 2
    Last Post: 12th October 2007, 00:07
  4. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 21:01
  5. Problem with dynamic_cast
    By vratojr in forum General Programming
    Replies: 7
    Last Post: 12th April 2006, 14:45

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.