Thread: An issue with Switch (Scope?)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by serge
    But still it should be possible to recast pointers of unrelated classes, I just can' wrap my head around how:
    Doing that reinterprets an object of one type as being of another type via pointers, and isn't what you want here. What you're probably thinking of is a pointer to void, but that is not type safe, and anyway that isn't going to work with a shared_ptr. (You could get a pointer that could be converted to void* from a shared_ptr, but once the shared_ptr goes out of scope, that pointer to void becomes invalid.)

    Even if you switched from a shared_ptr so as to use a pointer to void, you have other problems: you could pass the pointer to void to PrintPlayer, but PrintPlayer would have no clue as to what is the underlying type, so you would need some way to specify it, and you'll be hardcoding PrintPlayer (and every other "generic" Player function) for each possible subclass of Player, whereas if you used polymorphism, you could add new Player subclasses without having to update PrintPlayer.

    Why are you trying so hard to avoid better design?
    Last edited by laserlight; 10-18-2019 at 11:32 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch case variable scope
    By VirtualAce in forum C# Programming
    Replies: 7
    Last Post: 02-01-2014, 08:45 AM
  2. Scope issue with header file used as interface
    By Rath in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 05:04 PM
  3. Case Switch issue
    By pc_doctor in forum C Programming
    Replies: 8
    Last Post: 12-06-2008, 06:24 AM
  4. Basic scope issue
    By Dondrei in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2008, 06:36 AM
  5. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM

Tags for this Thread