Advertisement

friend functions

Started by December 31, 2002 12:30 AM
4 comments, last by Metal Typhoon 21 years, 10 months ago
i''m getting a compiler error when i try to acess a variale in private from a class :O i know friends can acess private but it jsut gives me this : error C2065: ''p_Hdc'' : undeclared identifier.. here is the class def.
  
class AP16OGL
{
        public:
		int m_Depth;
		int m_Color;
		friend HDC   UseDc ();
		friend HGLRC UseRc ();
		friend HWND  UseHn ();
		friend HDC   UseDc (HDC Hdc);
		friend HGLRC UseRc (HGLRC Hrc);
		friend HWND  UseHn (HWND Hwnd);
	private:
		HDC   p_Hdc;
		HGLRC p_Hrc;
		HWND  p_Hwnd;
};


HDC UseDc ()
{return p_Hdc;}

HGLRC UseRc ()
{return p_Hrc;}

HWND UseHn ()
{return p_Hwnd;}

HDC UseDc (HDC Hdc)
{p_Hdc = Hdc;
return p_Hdc;}

HGLRC UseRc (HGLRC Hrc)
{p_Hrc = Hrc;
return p_Hrc;}

HWND UseHn (HWND Hwnd)
{p_Hwnd = Hwnd;
return p_Hwnd;}
  
if i change like return p_Hdc to AP16OGL:_Hdc it gives me this -> error C2597: illegal reference to data member ''AP16OGL:_Hdc'' in a static member function. HOw can i acess those ?? thx in advance
Metal Typhoon
Put the class into a header file and define the functions in a C/CPP with the header included.
Advertisement
It looks to me like what you want is member functions, not friend functions. Have you ever used classes and objects in C++ before?
Why are you using friend functions here? If you need to access members of a class, then use member functions for that.

Friend functions are not part of a class. They can access a class'' members through a pointer (which you don''t have) or a class'' static members. So, either make your p_Hdc member static (if you only have one instance of your class at any given time. i.e. it''s a singleton) or make the friend functions into member functions.

Kippesoep
quote: Original post by Kippesoep
Why are you using friend functions here? If you need to access members of a class, then use member functions for that.

Friend functions are not part of a class. They can access a class'' members through a pointer (which you don''t have) or a class'' static members. So, either make your p_Hdc member static (if you only have one instance of your class at any given time. i.e. it''s a singleton) or make the friend functions into member functions.



it''s because it''s for a DLL file... and i want the dll to use the functions but dont export in case i decide to export th class
Metal Typhoon
You will still have to pass an AP16OGL object to the function - as it has been pointed out, they are not member functions :

class AP1OGL{  ...  friend HDC UseDc ( const AP16OGL& obj );  ...};HDC UseDc ( const AP16OGL& obj ){  return obj.p_Hdc;} 


Your choice of identifier is quite error prone - is that a 1 or an I ?

You shouldn''t use friend functions like that, it''s fugly.

You should make the DLL functions that USE the private members friends. That way, the class internals are not exposed to non-friends, which is as it should be.
What you did is near-equivalent to making the members public, and a total violation of encapsulation principles. Making the members public would be more honest.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"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

This topic is closed to new replies.

Advertisement