Closed Thread
Results 1 to 3 of 3

Thread: A question about custom type casting

  1. #1
    jackolantern's Avatar
    jackolantern is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    47
    Rep Power
    0

    A question about custom type casting

    Something doesn't really make sense to me regarding custom type casting. The book I am reading over explains that a derived type can always be implicitly cast to a base type, while a base type must be explicitly cast to a derived type.

    While I understand the concept, and it is easy enough to follow, I don't understand the logic behind this. It seems counter-intuitive to how the compiler works works with widening and narrowing casts. You can implicitly cast a short to an int, because the int holds at least as much as a short, but you must explicitly cast an int to a short because the int can hold more and you may lose data. With custom types, the derived type will hold at least as much data as the base type. For example, what if the derived type has additional fields added to it beyond what it inherited from its base type? You can implicitly cast it to the base type, so it could happen without you really being aware of it.

    I am just not sure how to wrap my head around the logic here. Thanks!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: A question about custom type casting

    I've been reading The Design and Evolution of C++, so can maybe shed some light on this...
    Going from short to int does not risk data loss, whereas going from int to short does. As a result, short to int is automatic.
    Now consider a mammal class that has two subclasses, dog and cat. Now consider the following code snippet:
    Code:
    Cat* mycat = new Cat;
    Dog* mydog;
    Mammal* animal;
    animal = mycat;
    mydog = animal;  //throws error... and well it should!
    Because the Mammal interface has all the necessary information to be certain that it won't access non-existent data in mycat, the assignment to animal is fine. However, Dog may add features that were NOT added to Cat (such as the growl() method and the AKCregistered bool flag). When you attempt to access those (non-existent) entities in mydog, there's no telling what might happen.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    jackolantern's Avatar
    jackolantern is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    47
    Rep Power
    0

    Re: A question about custom type casting

    Ok, that makes sense. Thanks!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 11
    Last Post: 01-20-2011, 12:52 PM
  2. Question: Type of programming used in Hypem.com (Hype Machine)?
    By obe1ben in forum General Programming
    Replies: 0
    Last Post: 12-24-2010, 01:19 AM
  3. Creating a Custom File Type...Need Help
    By deusprogrammer in forum C and C++
    Replies: 2
    Last Post: 03-13-2007, 12:41 AM
  4. Type Casting
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 06:32 PM

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