Thread: Wrong design?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Wrong design?

    I found myself in a position where it seems I need to use dynamic_cast but I would like to avoid that. What I have is my base class called Sound and a bunch of derived object called PointSound, PlaneSound, etc. The problem is that from a different part of the program I have an stl map with pointers to different Sound objects which cound be PointSound, PlaneSound or whatever, and I need to have the actual type of the Sound. So for example I would do
    Code:
    Sound *currentSound = this;
    PlaneSound *mySound = dynamic_cast<PlaneSound*>(currentSound); //currentSound is type Sound.
    if(!mySound)
       return NULL;
    
    return mySound;
    But I want to avoid using dynamic_casts or having to enable RTTI due to performance issues. The only solution I could come up with is just plain horrible, I would create a virtual function in Sound called isPlaneSound() and would return a valid PlaneSound pointer if the Sound is in fact a PlaneSound. But that just horrible since I would have to do the same for each type and I code would be just horrible to maintain.

    I hope someone can help me, thanks in advance.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    typeid several approaches

    dynamic_cast good solution

    Kuphryn

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by kuphryn
    typeid several approaches

    dynamic_cast good solution

    Kuphryn
    I'm trying to avoid both of those solutions due to performance issues. This is a real-time application.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    "whoo whooo" that made me laugh for some reason heh.
    That's not my problem, PlaneSound(the name comes from a geometricaly planar sound source, not from an actual plane) and PointSound, etc. don't have the same functions and data except the data they share from the base class Sound. I'm trying to get a pointer to the right type. Thanks anyway.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It really depends on what the function that you are currently using dynamic_cast in is supposed to do. If it is supposed to make a sound, for example, then perhaps elad's idea is a possible solution.

    You might also have a virtual function in the Sound class that creates a helper class. You would create a base class helper and derived class helpers for each specific class that you need. Then, you could call the GetHelper method from currentSound to get the helper class, and use the helper class' virtual functions to get the specific functionality.

    BTW, in your code example, you are using dynamic_cast on this. You should never really need that, since you can just call a virtual function and the derived class should be able to do the work.

    Also, they shouldn't be derived from the same class if they don't have the same interface.
    Last edited by Daved; 06-26-2006 at 01:52 PM.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by Hulag
    Code:
    Sound *currentSound = this;
    PlaneSound *mySound = dynamic_cast<PlaneSound*>(currentSound); //currentSound is type Sound.
    if(!mySound)
       return NULL;
    
    return mySound;
    This bit of code is bad design.
    Since you are using this I assume it is a member function. You never need a dynamic cast in a member function, if it is a mamber function of PlaneSound it knows this is PlaneSound*

    If the function is meant to be a virtual function that renturns the corrsect somethingSound* it will not work. The base class will have this function in its signature with a certain return type and since the code will be called through that signature all the subclasses will return that type too.

    If you are really that concerned about performance you could give your base class a "type" function that returns an identifier unique to each subclass (say PlaneSound returns 1 and PointSound returns 2) and use a switch statement to select the correct code. Each case would then start with a static_cast<someSound*>. static_cast shoudl be safe there becasue you know it is casting to the correct type.

    Personally unless the number of sound types can vary at runtime I would go with the option of writing a number of member functions that do the downcast to each specific type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  3. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  4. are you bored? (interiour design questions)
    By maes in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-04-2004, 04:51 AM
  5. GUI Design for the first time!
    By westie in forum C Programming
    Replies: 5
    Last Post: 11-22-2001, 11:45 PM