-1

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
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • 4
    I don't see what the title of your question has to do with the body of your question. – inetknght May 14 '13 at 23:39
  • And after fixing the question title, maybe you can post a small example of what you're taking about (but just an example - it sounds like your actual code is quite a maze). – Michael Burr May 14 '13 at 23:41
  • I have added my project – Jimmyt1988 May 14 '13 at 23:50
  • @all Cheers for all your help guys, you helped me out massively, I am going through more confidently now with passing references through. I needed a nice kick up the botty. – Jimmyt1988 May 15 '13 at 09:45

2 Answers2

0

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • So do I need to create 2 new variables in my Game class for Font and Viewport and then pass gfx into them? – Jimmyt1988 May 14 '13 at 23:53
  • @JamesT: there are quite a few different ways to handle things. It sounds to me like you're *probably* over-using friendship, and probably need to define your public interface to supply what's needed. The main use of friends is for things that are conceptually member functions, but (for example) have to be free functions to support conversions on their left operands. – Jerry Coffin May 14 '13 at 23:56
  • Is it usual to have lots of private members of a class, for example my Game class and then pass things into them so that they have scope inside the Game class? As I have never seen someone elses code, I am not sure if I am doing things okay? – Jimmyt1988 May 14 '13 at 23:57
  • Better question is, is there rules for when you shouldn't use friends.. What is the overhead of doing friend stuff rather than passing everything through to every outside class required? I feel that viewports and fonts are relative to D3DGraphics but.. does that mean they should be friends? – Jimmyt1988 May 15 '13 at 00:00
  • @JamesT: No, it doesn't mean they should be friends. Rather the contrary, something like D3D graphics is the natural client of something like a viewport or a font, so what it needs to use should probably be in their public interface. – Jerry Coffin May 15 '13 at 00:02
  • Let me update my code with what I think it should be in that case, perhaps you could take a look and give me the heads up. This would infact confirm something very important in my head about C++... it's okay to push things around like cheap hookers. – Jimmyt1988 May 15 '13 at 00:03
  • Is this more like it... is this okay to do: https://github.com/jimmyt1988/TheGame/tree/master/TheGame You can see that I am now creating instances in game.h and define default value to constructor and then passing gfx through to the class. – Jimmyt1988 May 15 '13 at 00:19
  • regardless of if the above is okay, i'm going to do this from now on. I'll keep my project shared on github and maybe someone might tell me if i'm doing it right or not.. It seems to work well now I have also incorporated my viewport! – Jimmyt1988 May 15 '13 at 00:49
0

If your code looks something like

class Font {
  ...
  friend class D3DGraphics;
  ...
}

then it's not going to work without

class D3DGraphics {
  ...
  friend class Font;
  ...
}

Friendship is not reciprocal. If A declares B a friend, A objects get no special privileges to B objects.

(By the way, where are the contents of Font.h? The file has appeared, and supports my hypothesis.)

Also, D3DGraphics.h and Font.h are circularly dependent, and Font.h has no include guard; including it first will include it twice, which may result in compiler complaints about redefinition of class Font.