Thread: Considered bad design?

  1. #1
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Considered bad design?

    I have two classes, CSprite, and CSpritePalette... CSpritePalette contains bitmap information to animate a CSprite object. To instantiate a CSprite object you must pass a CSpritePalette pointer to it.

    I choose this design instead of combining the CSprite and CSpritePalette into a single class because it's more efficient. If I have two sprites with the same animation on the screen, I only create one copy of the bitmaps.

    Here's the potential problem. When the CSpritePalette pointer is passed to the CSprite object, the address is saved internally to continue loading bitmap data at a later time. If the CSpritePalette goes out of scope or gets deleted... well, goodbye program. I've read that it's bad design to save the address of a pointer for which the future of is unknown.

    Anyway, does anyone see a problem with doing it this way? If i'm careful i'll keep the CSpritePalette in scope while using the CSprite object... but it still worries me. Any alternitives or safeguards I can implement to rid of this hazard?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Don't be silly. This is no different from other scoping issues. Just make sure you keep it in scope (or else dynamically allocate it)...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    or have your class use smart pointers so that the last one with the reference to the object in memory will clean it up.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Example?
    Or would this be incrementing some static variables? If so, then I think I get the idea.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Something like this, maybe?

    Code:
    class CSP {
    CSP():ref_count(0){}
    int ref_count;
    void Attach(){
    ++ref_count;
    }
    void Detach(){
    --ref_count;
    if(ref_count <= 0)
    //...delete this; //  <--never tried that before! Not sure.
    }
    };
    
    
    class CS {
    CSP * CSP_ptr;
    CS(CSP& address){
    address.Attach();
    CSP_ptr = &address;
    //...
    }
    ~CS(){ CSP_ptr->Detach(); }
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 11:42 PM
  2. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 09:26 AM
  3. 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
  4. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM
  5. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM