Thread: Requesting Affirmation [pointer 2 custom class]

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    73

    Requesting Affirmation [pointer 2 custom class]

    Boy, that dictionary is paying off

    Anyways.

    Heres my problem:

    I have a custom class I created called CTexture as well as a custom class called CSprite. What I need is for multiple CSprite classes to point to a SINGLE CTexture class.

    Heres my two classes.
    Code:
    class CTexture
    {
      public:
        CTexture();
        ~CTexture();
        LPDIRECT3DTEXTURE9 GetTexture();
        int GetWidth();
        int GetHeight();		
        bool LoadTexture(char *filename, int width, int height, LPDIRECT3DDEVICE9 device);
    
      private:
      LPDIRECT3DTEXTURE9 m_texture;
      int m_width;
      int m_height;
    };
    Code:
    struct SFrame
    {
      int frameNum;
      RECT frameBoundary;
      D3DXVECTOR3 center;	
      SFrame *nextFrame;
    };
    
    class CSprite
    {
      public:
        CSprite();
        ~CSprite();
        bool SetTexture(CTexture &texture);
        void NewFrame(int frameWidth, int frameHeight, int startPixelX, int startPixelY);
        bool Draw(int frameNum,  D3DXVECTOR3 position, LPDIRECT3DDEVICE9 device);
    
      private:
        CTexture *m_Texture;
        SFrame *m_FirstFrame;
        LPD3DXSPRITE m_Sprite;
        int m_TotalFrames;
    };
    
    bool CSprite::SetTexture(CTexture &texture)
    {
        if(!texture.GetTexture())
           return false;
        
        m_Texture = &texture;
    	return true;
    }
    I was just wondering if my line in RED is correct or not. I'm pretty sure it is and by cycling through the debugger abit it appears to do what I want it to do. However, since this wasn't my initial solution I just wanted to get a second opinion if this is proper or not.

    My initial solution was:

    Code:
    m_Texture = texture;
    But it was complaining about "=" because I didn't overload the "=" for CTexture class. If I created a "=" function and copied variables from texture to m_Texture that would create a copy and not a pointer right?

    Because elsewhere in my code I have:
    Code:
    SFrame *m_FirstFrame;
    SFrame *temp;
    temp = m_FirstFrame;
    And it works fine.. seems like a similar circumstance except temp & m_FirstFrame are referring to a STRUCT while in the first case I had pointers to a CLASS.. Why does the first example require a & but the second case does not complain about a need for overload "=" operator.

    Is this because a class has private while everything in a struct is public?

    Thnx for any clarification.
    Last edited by Deo; 06-07-2005 at 03:22 PM.

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    And it works fine.. seems like a similar circumstance except temp & m_FirstFrame are referring to a STRUCT while in the first case I had pointers to a CLASS.. Why does the first example require a & but the second case does not complain about a need for overload "=" operator.
    In the second case, both variables are of the same type: SFrame *

    In the first case, the types are distinct ( CTexture& != CLTexture *). Your code seems ok, but I would make CSprite::m_Texture a reference, and not a pointer. If you do this, you will need to initialize the variable at the constructor:
    Code:
    CSprite::CSprite(CTexture& t)
    :m_Texture(t)
    {
       //code
    }
    Do not forget to change the m_Texture type to & instead of *.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    73
    Thnx for the info...

    Your code seems ok, but I would make CSprite::m_Texture a reference, and not a pointer.
    Ok, your the boss... will do... just one quick question. If I were to make this variable a reference and not a pointer what would happen when my CSprite variable goes out of scope and the destructor is called. Since my m_texture is a reference wouldn't that then call the destructor for my CTexture class?

    Just wanted to make sure it wouldn't as I will have one CTexture class shared among multiple CSprite classes.
    Last edited by Deo; 06-08-2005 at 11:03 AM.

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Yes, it will. I am sorry I have not seen this before. You are completely right. Change the references to pointers. I would sugest still use the constructor to set the texture, if this is not possible use the method as before. Again, I apologize for my mistake telling you to use references.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, if m_Texture is a reference the CTexture destructor will NOT be called.

    The decision of whether to make it a reference or a pointer should be based on whether you ever need it to be null, or whether you ever need to change the m_Texture for a specific CSprite instance. If neither will happen, a reference is fine. Otherwise, your original code that sets the pointer is fine.

    I assume you are managing the memory containing the CTexture instances somewhere else. If so, either choice should work, just don't delete the CTextures until the CSprites are no longer used.

  6. #6
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Uhm... I thought that would be destroyed. Thanks for the correction! . What a shame again!

Popular pages Recent additions subscribe to a feed