c++ - Benefit of copying pointer class arguments to class members? -
i have player class uses directx graphics class create surfaces , draw sprites.
i passing graphics class pointer player class methodlike this:
**game.cpp** m_pplayer->init(graphics* graphics);
inside player class init method passes pointer pointer method. there benefit creating graphics pointer in player class , copying pointer argument instead?
so this:
**player.cpp** m_psurface->init(graphics, m_width, m_height);
vs this:
**player.cpp** m_pgraphics = graphics; m_psurface->init(m_pgraphics, m_width, m_height);
i know having m_pgraphics pointer allows me reuse pointer 1 time again in same class, such drawing method phone call method in player class anyway can't maintain passing pointer on , on again?
this matter of style , unlikely have effect on performance. copying pointers inexpensive , leave compiler optimize.
if player
uses graphics
object great deal reasonable store pointer in fellow member variable instead of passing in time. may create fellow member function signatures on player
shorter good.
you need sure fellow member pointer graphics
object remains valid whenever used. still right graphics
object , still alive? if pass pointer every time might not have complication.
c++ pointers directx directx-9 member-pointers
No comments:
Post a Comment