Results 1 to 4 of 4

Thread: Cast problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Cast problem

    Hi,

    I have a base class called CItem and 3 derived class called respectively CItemSound, CItemImage and CItemColor.

    My application uses a QValueList<CItem>objList member to manipulate all my objets (of type CItemSound, CItemImage and CItemColor) regardless to their type.

    I declare the objList member as follow :
    Qt Code:
    1. QValueList<CItem>objList;
    To copy to clipboard, switch view to plain text mode 
    and I fill it as follow :
    Qt Code:
    1. CItemSound s1;
    2. s1.setItemName("Sound 1 of list 1");
    3. CItemImage i1;
    4. i1.setItemName("Image 1 of list 1");
    5. CItemColor c1;
    6. c1.setItemName("Color 1 of list 1");
    7. list1.append(s1);
    8. list1.append(i1);
    9. list1.append(c1);
    To copy to clipboard, switch view to plain text mode 

    Let me explain my problem now :
    in a function I need to do something like this :
    Qt Code:
    1. CItemSound s = objList[0];
    To copy to clipboard, switch view to plain text mode 
    ... it fails ... how is it possible to do such a cast ?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Cast problem

    You are trying to assign a CItemSound variable the contents of a CItem variable. That is never valid because the compiler can't set all fields of the derived class.

    You have another problem: QValueList stores by value: That means all your classes get "sliced" to fit into a CItem base class. You simply lose all other information they contain. Store instead pointers to instances of your classes - these you can cast any way you want. But ensure that you don't cast a CItemImage to a CItemSound.

  3. #3
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Cast problem

    hmm, ok, I try right now

  4. #4
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Cast problem

    ok, that's fine.

    Thanks

Similar Threads

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