Help with friend functions!

Started by
4 comments, last by SSJCORY 20 years, 6 months ago
Ok i''ve never used friend functions before i dont know if this is how you use them... If not could someone show me how i would get this to work...

#include<iostream.h>
class player{
private:
	int mynumber;
public:
	int getnumber(){
		return mynumber;
	}
	void printnumber(){
		cout<<getnumber();
	}
	void setnumber(){
		mynumber = 10;
	}
	friend class enemy;
}help;
class enemy{
private:
	int health;
public:
	friend int player::getnumber();
	void getotherplayerhealth(){
		cout<<" " << getnumber();
	}
}enemy;
int main(){
	help.setnumber();
	help.printnumber();
	enemy.getotherplayerhealth();
	return 0;
}
Help is extremely appreciated... I''ve tried doing friend functions like 5 different ways... None have worked. Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
Maybe i just dont understand what the friend functions actually are?


Favorite Quotes:Gandalf: You shall not pass!|Gollum: We don''t need you |Smeagol: My little hobbitses.|
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
If what I am gathering from your code is correct, you are trying to access the function Player::getnumber() within the enemy class?

When you declare a class to be a friend of another class, you are declaring that the friend class is allowed to access private members of the class that declared it as a friend. A working example along the lines of what you are trying to do would be this:

#include <iostream>class CPlayer{friend class CEnemy;public:	void SetHealth (int health)	{		m_Health = health;	}	void GetHealth ()	{		std::cout << "Player health: " << m_Health << std::endl;	}private:	int m_Health;};class CEnemy{public:	void SetCurrentPlayer (CPlayer *p)	{		m_Player = p;	}	void GetPlayerHealth ()	{		std::cout << "Player health (From inside CEnemy): " << m_Player->m_Health << std::endl;	}private:	CPlayer *m_Player;};int main (){	CPlayer player;	CEnemy enemy;	player.SetHealth(10);	player.GetHealth();	enemy.SetCurrentPlayer(&player);	enemy.GetPlayerHealth();	return 0;}


You seem to be confused thinking that by declaring a function to be a friend, you can call a function directly from another class. That is not the case. All friend classes and functions can do is access private variables in the class they are befriended by. Does that clear anything up?
"...."
I don''t know if this applies here, but if you are using friends, reanalyze your design and make sure you need them. Personally, I find things like that to generally be bad practice, and sometihng I do when I''m lazy and don''t want to code accessors and what not. This isn''t to say you should never use friend; just give it some thought.
Peon
class C_GLVertex{   friend class C_GLTexQuad;   friend class C_GLTexTriangle;   friend class C_GLLine;   friend class C_GLPoint;         public:          C_GLVertex( void );       C_GLVertex( const C_GLVertex& );      ~C_GLVertex( void );         protected:         float x;      float y;      float z;            float red;      float green;      float blue;      float alpha;                           private:   };


I figured that declaring the classes as friends instead of accessors might be faster. Mabe this inline accessors ae the same
speed. Idont know I dont have a profiler. And i''m sleepy right now.
Thanx guys I''m gonna look over that code and see what i can figure from it.


Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you |Smeagol: My little hobbitses.|
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|

This topic is closed to new replies.

Advertisement