Skip to main content
gamedev.net
🔒 Locked

Friend Class

Started by dirkduck 0 replies 150+ views
Original Post
dirkduck
dirkduck
Hello everyone. I''ve never used friend classes before, but just today I found (a very large) use for them in a project of mine. Anyway, what im doing is this:
  
class test
{
public:
	int tester;
};
	


class Cblah
{
public:
	int type;
	friend test;
};

...(later on)
Cblah bob;

...(later)
if(bob.tester==1)
{
    ...
}
  
But when I try to compile it, it tells me that ''tester'' is not a member or Cblah. Now, it isn''t, but shouldn''t the friend to test allow it to access ''tester''? I may be totally wrong about how friends work, so could someone tell me either what im doing wrong or set friends straight? Thanks. http://www.labino.net
http://www.labino.net
Fruny
Fruny
- You must write friend class test

- Making test a friend of Cblah means that test can access the private members of Cblah (call private/protected functions, access data ...) not that Cblah has a test member.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.