Considered bad design?

This is a discussion on Considered bad design? within the C++ Programming forums, part of the General Programming Boards category; I have two classes, CSprite, and CSpritePalette... CSpritePalette contains bitmap information to animate a CSprite object. To instantiate a CSprite ...

  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,699
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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,699
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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, 10:42 PM
  2. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 08: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, 03: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, 07:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21