Search code examples
c++scopefriend

C++ variable scope for class friends


I have:

class Game... 
class D3DGraphics...

I have a variable of type D3DGraphics called gfx declared in my Game class.

I make another few classes:

class Font...
class Viewport...

I make them both friends of D3DGraphics and declare variables inside D3DGraphics class:

Font font;
Viewport viewport;

Finally in my D3DGraphics I have a variable made public:

LPDIRECT3DDEVICE9 d3dDevice;

my font class cannot see d3dDevice even though it is a friend of D3DGraphics?

Why is that?

EDIT::// Have now changed code around to push pointers through to outside classes: https://github.com/jimmyt1988/TheGame/tree/master/TheGame

  • windows.cpp // where I create my game object and pass my hWnd through.
  • Game.cpp / Game.h
  • Font.cpp / Font.h
  • D3DGraphics.cpp / D3DGraphics.h
  • Viewport.cpp / Viewport.h

Solution

  • At least if understand correctly, you're expecting friendship to be transitive -- i.e., A is a friend of B and B is a friend of C, so you're expecting A to be a friend of C.

    If so, the simple answer is because (as C++ defines it) friendship is not transitive.