Results 1 to 14 of 14

Thread: Can I cast a pointer from a unknow type ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Can I cast a pointer from a unknow type ?

    As high_flyer mentioned, you not very clear in how inheritance in C++ works, (or at-least how to use it, in your case).

    Qt Code:
    1. Baseclass *entity = new Baseclass;
    To copy to clipboard, switch view to plain text mode 
    This is not correct, you should be using new DerivedClass, or a factory method to create object and get it's base class pointer. If you do it this way, you are basically creating a BaseClass Object and store it in BaseClass pointer, where as you want to create a DerivedClass object, and store it in BaseClass pointer.

    *entity = *dynamic_cast<(typeid(m_listEntities.at(m_listEnti ties_selected[i]) ) )> (m_listEntities.at(m_listEntities_selected.at(i))) ;//errors
    This is not possible, you need to know the type at compile time to cast it to, where as typeid operator returns a type_info class object (which happens at runtime), and again which is of type type_info, not your BaseClass (as you expect)

    you can use the typeid operator to get type information like, this

    typeid(int).name()
    typeid(derived).name() //parameter can be object, class, or object pointer..
    if(typeid(object1) == typeid(object2)) //this is run time object creation, and uses the type_info class == operator to compare the objects
    ...
    Conclusion:
    To create a DerivedClass object, you cannot avoid if..else (or switch case), to know the type(or other criteria) to create object. The best practice is either have factory class, or a factory method, which is the solution provided by the factory design pattern for your problem.

    I suggest you reading about factory design pattern, which is cleaner, maintainable (adding new derived classes in future) solution to your problem.

  2. The following user says thank you to Santosh Reddy for this useful post:

    john_god (26th May 2011)

Similar Threads

  1. Replies: 1
    Last Post: 4th December 2010, 17:20
  2. Pointer of an unknown type...
    By TheNewGuy in forum Newbie
    Replies: 1
    Last Post: 17th December 2009, 08:50
  3. Dynamic pointer type
    By estanisgeyer in forum General Programming
    Replies: 3
    Last Post: 9th October 2008, 16:51
  4. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58
  5. cast
    By mickey in forum General Programming
    Replies: 1
    Last Post: 12th July 2006, 11:10

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.