friend class declaration

I noticed the following piece of code
1
2
3
4
5
6
7
8
9
10
11
12
13
//#include "Alien.h"//
  #pragma once
class Monster
{
	friend class Alien;
public:
	Monster();

	void scratch(Alien*);

	~Monster();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include "stdafx.h"
#include "Alien.h"
#include "Monster.h"

int main()
{

	Monster m1;
	Monster* m2;
	Alien a1;
	Alien* a2;
	 
   a1.poison(m2);
   m1.scratch(a2);

	_getch();
	return 0;
}


Whats making me scratch my head is that how does the class Monster know about the class Alien without me including the header file .Is this normal for the vs to know about where the friend class Alien is without including headers?
I am running this code under vs 2010

Thanks
Last edited on
friend class Alien; tells the compiler that a class named Alien is a friend of the Monster class. At this point it doesn't need to know anything more about the Alien class.
but why does it show an error for the following declaration of a friend function which is defined properly in the cpp file :

1
2
3
4
5
6
7
8
9
10
11
12
//#include "Alien.h"//
  #pragma once
class Monster
{
	friend void Alien::poison();// error
public:
	Monster();

	void scratch(Alien*);

	~Monster();
};


Last edited on
closed account (SECMoG1T)
I guess when you declare a friend class ... then during compilation you compiler should be able to know where the friend class or function is located or declared

If you compiler is able to compile the code without you providing the headers of where your friend class is defined then that sounds like an error to me ..... I would also like to here about that from a more exprienced programmer ...

The error you got from you recent post is what should happen if you don't provide the headers or a prototype
Last edited on
anyone?
This friend class Alien; has only one meaning: there is a class Alien somewhere and it is a friend of this class.

This friend void Alien::poison(); can have many meanings:

What is an Alien::poison?
Is it member function of Alien class?
Is it static function of Alien class?
Is it standalone function in Alien namespace?

So, without extra information this is an error
yes it is a memeber function of the alien class
no its not static
i didnt create a namspace
How would a compiler know that?
I think this is a bit similar to when you define a member function you have to first declare it in the class definition. So it's just consistent with that. If the class has not been defined yet, or the class does not contain the function then you can't refer to the function.
Last edited on
so we can define a whole class to be a friend without including a header file
but not any of its perticular function whether its public or private ,right?
Pay attention to the difference between a declaration and definition.

A declaration tells the compiler that there is something.
1
2
3
// declarations:
class A; // there is a class named A
void foo(int); // there is a function named foo that takes an int as argument 

A declaration tells the compiler what something is. (Note that a definition is also a declaration)
1
2
3
4
5
6
7
8
9
10
11
12
// definitions:
class A
{
public:
	void doSomething();
private:
	int b;
};
void foo(int value)
{
	std::cout << value << std::endl;
}

vxk wrote:
so we can define declare a whole class to be a friend without including a header file
but not any of its perticular function whether its public or private ,right?

Yes that's right. To be able to declare a member function as friend you need the class definition.
is forward declaration of the class also a correct way ?

class Monster
{
class Alien;
friend void Alien::poison();
.....
.....
};
No. You cannot declare class member as a friend without class definition.

In this case for example there is no information about whether Alien::position() is a static function or not.
1. A class declared at class scope of another class declares a nested class.
1
2
3
4
class Monster
{
      class Alien; // this declares class Monster::Alien 
      ...


This is an error:
1
2
3
4
5
6
7
class Alien { public: void poison() ; } ; 

class Monster
{
      class Alien; // this declares class Monster::Alien 
      friend  void Alien::poison(); // *** error: incomplete type 'Monster::Alien' (declared, but not defined)
};



2. It is therefore evident that a friend declaration does not declare the type.
1
2
3
4
5
class A
{
    friend class B ; // this is a friend declaration; it is not a declaration of class B
    B* pointer = nullptr ; // *** error: unknown type name 'B'. 'B' does not name a type
};


http://coliru.stacked-crooked.com/a/b40043240796aae5
Topic archived. No new replies allowed.