Results 1 to 11 of 11

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

Hybrid 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 Re: Is the down casting right, and why dynamic_cast fails?

    And what's the best way to cast, if it's like in my code. you have a pointer Base* which is actually a pointer of MyList<Base*>* and you want to cast to MyList<MyString*>*. Do we have to do it in two steps?
    Does anybody know an answer to the question above?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

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

    Your code is more than a little bit confusing. Why do you use Base as the base class for both the list -and- the things in the list? C++ inheritance is intended to express "is-a" relationships: a MyString is-a Base. But your code also claims that MyList< Base * > also is-a Base. You're trying to use this empty, bogus Base class so you can cast things to each other that can't really be cast.

    A MyList< Base * > is a different class than a MyList< MyString * >, even if the contents of each list can be up or down-cast to each other.

    A static cast works because a static cast says, "I don't care if the thing I am trying to cast is completely unrelated to the thing I'm trying to cast it to, just do it", so of course it works for your case. Dynamic casting doesn't work because the two list types are unrelated even if their contents are. At best, you might be able to dynamically cast the MyList< Base * >* to a Base *, and then dynamically cast that to a MyList< MyString * >*, but then your code says you could also cast it to a MyString * (which of course it isn't) by virtue of the confused inheritance tree you've defined.

  3. #3
    Join Date
    Nov 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    9

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

    Thank you for your explanation. But what's the reason to use two dynamic casts instead of two static casts(or maybe one static cast). I thought I need dynamic casts just in cases, where I'm not sure if the pointer holds the to-casting-pointer or not at runtime.

    In serialisaton (i.e xml-communication between client and server)implementations I found such an inheritance design. A list should be serializeable and a string too, so they are derived classes from one base class.
    Last edited by QtCrawler; 11th December 2015 at 14:07.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

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

    In serialisation (i.e xml-communication between client and server)implementations I found such an inheritance design. A list should be serializeable and a string too, so they are derived classes from one base class.
    That's a quite different use of the concept. Just like in Qt, all classes that can have properties, signals, slots, and so forth inherit from QObject, in the serialization example, they undoubtedly inherit from a base class that defines the methods required by a serializable object. The base class isn't there to provide a workaround for anything, it defines an interface which all serializable objects must implement, and establishes a true "is-a" relationship for derived classes.

    Your "Base" class doesn't define anything, doesn't represent anything other than an empty name. You aren't using it to define the behavior and semantics of a class hierarchy, you're trying to force an unnatural relationship between classes that aren't related. This isn't how inheritance in C++ is intended to be used.

    Static casting does no type-checking, no verification that the first type can actually be cast to the second type. It's a brute force way to do something that is generally unsafe and often incorrect. If dynamic casting won't work, it means you are trying to do something that shouldn't be done, and so you shouldn't be using a static cast to force the compiler to accept it.

  5. The following user says thank you to d_stranz for this useful post:

    QtCrawler (14th December 2015)

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.