Results 1 to 8 of 8

Thread: Problem with dynamic_cast

  1. #1
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with dynamic_cast

    Hi,even if the following code involves a bit of Qt I post it here because I think it's a more general problem.
    The situation is the following:

    I have a class that know its position inside an array:
    Qt Code:
    1. class element{
    2. public:
    3. int position(){return pos;}
    4. private:
    5. int pos
    6. }
    To copy to clipboard, switch view to plain text mode 
    A widget that contains it:
    Qt Code:
    1. class widget : public QWidget{
    2. public:
    3. widget(element *elem):el(elem){};//I also pass parent to the constructor of qwiget
    4. element* element(){return el;}
    5. private:
    6. element* el;
    7. }
    To copy to clipboard, switch view to plain text mode 

    The principal widget is a QTabWidget and it contains a vector of elements.
    Qt Code:
    1. class main : public QTabWidget{
    2. private:
    3. QVector<element> elements;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Using some slots, I create some elements and add them to the QVector (and coherently I set theirs pos memebers) and then use them to initialize widgets and add them to the tabwidget.
    Qt Code:
    1. //inside main
    2. addTab(new widget(&elements[i]);
    To copy to clipboard, switch view to plain text mode 

    Now,the problem. If from inside the tabwidget I want to access to *el inside widget (inside a particular tab) I have to use a dynamic cast.
    So I use:
    widget *w = dynamic_cast<widget*>(QTabWidget::widget(k));
    if I then use w->position() to get its position, I sometimes get strange numbers.

    It seems that,during the cast the pointer *el inside widget, became dangling...why?

    Thanks!
    Last edited by vratojr; 11th April 2006 at 17:44.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with dynamic_cast

    Quote Originally Posted by vratojr
    It seems that,during the cast the pointer *el inside widget, became dangling...why?
    Did you add something to that elements vector in the mean time?

  3. #3
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with dynamic_cast

    You mean "after the cast"? No,I didn't modify anything.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with dynamic_cast

    Quote Originally Posted by vratojr
    You mean "after the cast"? No,I didn't modify anything.
    I meant this line "addTab(new widget(&elements[i]);". Do you add anything to that vector after creating those widgets?

  5. #5
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with dynamic_cast

    I APPEND other elements and I creat new widgets,nothing else.
    Each time I create an element, I set the position variable and create the widget. nothing more.
    I add that,for what I see, I got errors only after creating the first widget. I mean, the first is ok,the others may be wrong.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with dynamic_cast

    Quote Originally Posted by vratojr
    I APPEND other elements and I creat new widgets,nothing else.
    Each time I create an element, I set the position variable and create the widget. nothing more.
    I add that,for what I see, I got errors only after creating the first widget. I mean, the first is ok,the others may be wrong.
    When you add elements to a QVector (or std::vector), it sometimes must allocate more space for new elements and to achieve that it might have to move all elements to another place in the memory. If this happens, all pointers to previous elements will be invalid.

  7. The following user says thank you to jacek for this useful post:

    vratojr (12th April 2006)

  8. #7
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with dynamic_cast

    Argh!Yeah! I've read this just some days ago...
    Thank you very much!

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with dynamic_cast

    You can either reserve enough space before you add elements (if you know their exact number) or use a vector of pointers.

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.