-
friend
I saw this header fil in some code I downloaded :
Code:
class car
{
friend void start();
friend void resume();
}
void start();
void resume();
I dont understand the use of the friend, whats the difference in functionality between the above and the below code ?
Code:
class car
{
void start();
void resume();
}
Appreciate any input.
-
The friend function is non-menber function that has the right to access provate data members of the class it has been declared to be a friend.
in the second code the functions you wrote are normal member functions.
What exactly is it that you are not understanding?
-
Code:
//header file
class Ccar
{
friend void Run(Ccar *);
protected:
Ccar(size_t Dummy = 0);
~Ccar();
virtual void Routine() = 0;
private:
queue<Ccar *>rqueue;
};
void Run(Ccar *);
//source file
void Run(Ccar *obj)
{
cout << "*Run*" << endl;
rqueue.push(obj); <--- rqueue undeclared !!!!!!
}
Whats the problem here ?
-
Code:
// header fil
class Ccar
{
queue<Ccar*>rqueue;
void Run(Ccar*);
friend void Run1(Ccar*);
protected:
Ccar(size_t Dummy = 0);
~Ccar();
virtual void Routine() = 0;
};
void Run1(Ccar*);
//source file
void Ccar::Run(Ccar*obj)
{
cout << "*Run*" << endl;
cout << rqueue.empty() << endl;
}
void Run1(Ccar*obj)
{
cout << "*Run1*" << endl;
cout << rqueue.empty() << endl;
}
If I use Run then I get 'implicit declaration of fuction Run(...)'
If I use Run1 then I get 'rqueue undeclared'
What am I doing wrong here ?
Thanks for any input.