Results 1 to 4 of 4

Thread: java "instanceof" in c++?

  1. #1
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default java "instanceof" in c++?

    Is there a similar procedure in c++ to the java "instanceof"??

    I would like to tell apart from the QObject::childs() list which widgets subclass a common base class i created for my subclassed widgets, but I haven't succeeded yet, the code I'm using right now is :

    Qt Code:
    1. void fetchChildData(QObject* vent){
    2. QObjectList childs = vent->children();
    3. for(int i = 0 ; i < childs.size() ; i++){
    4. displayWidget* dW = dynamic_cast<displayWidget*>(childs[i]);
    5. if(dW!=0){
    6. dW->fetchData();
    7. }
    8. fetchChildData(childs[i]);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    But it won't make the trick,... Any help would be really appreciated.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: java "instanceof" in c++?

    Have you tried QObject::inherits ?

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

    joseprl89 (26th March 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: java "instanceof" in c++?

    Thank you very much squidge!! It works perfectly!

    By the way, and just in case I need it in a project without using Qt, is there a way to do this without using QObject::inherits?

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: java "instanceof" in c++?

    C++ does have a typeid operator. An example can be found here: http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

    However this requires RTTI to be enabled in the compiler. Typically its disabled for performance reasons.

    In Qt, you can also use qobject_cast, which doesn't require RTTI support.

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 22:46
  2. Replies: 3
    Last Post: 15th February 2010, 18:27
  3. Replies: 3
    Last Post: 8th July 2008, 20:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

Tags for this Thread

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.