Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including header file in class that is a friend

Tags:

c++

class

friend

I was wondering if you have to #include "Class1.h" in a class that is using that as a friend. For example the .h file for the class that is granting permission to Class1 class.

class Class2 {
   friend class Class1;
}

would you need to #include "Class1.h" or is it not necessary? Also in the Class2 class, Class1 objects are never created or used. Class1 just manipulates Class2 never the other way around.

like image 326
JDN Avatar asked Apr 11 '12 20:04

JDN


2 Answers

The syntax is:

friend class Class1;

And no, you don't include the header.

More generally, you don't need to include the header unless you are actually making use of the class definition in some way (e.g. you use an instance of the class and the compiler needs to know what's in it). If you're just referring to the class by name, e.g. you only have a pointer to an instance of the class and you're passing it around, then the compiler doesn't need to see the class definition - it suffices to tell it about the class by declaring it:

class Class1;

This is important for two reasons: the minor one is that it allows you to define types which refer to each other (but you shouldn't!); the major one is that it allows you to reduce the physical coupling in your code base, which can help reduce compile times.


To answer Gary's comment, observe that this compiles and links fine:

class X;

class Y
{
    X *x;
};

int main()
{
    Y y;
    return 0;
}

There is no need to provide the definition of X unless you actually use something from X.

like image 64
Stuart Golodetz Avatar answered Sep 29 '22 04:09

Stuart Golodetz


No, you do not have to "#include "Class1.h" in a class that is using that as a friend." You don't even have to forward declare the existence of class Class1 either. You just have to declare Class1 as a friend inside Class2's definition and that's it! (I suppose the friend class Class1 statement alone acts as a kind of "forward declaration" of Class1 all by itself):

// Inside the definition of Class2, place this (usually
// at the very bottom inside the `private:` section)
friend class Class1;

Here's a full, working example. You can run and test it here (notice there are 2 tabs when running this example--one per file).

Class2.h:

#pragma once 

class Class2
{
private:
    int _myInt = 123;

    // Declare Class1 as a friend class to Class2 so that Class1 can access all of Class2's private
    // and protected members (as though Class2's members were part of Class1)
    friend class Class1;
};

main.cpp:

#include "Class2.h"

#include <stdio.h>

class Class1
{
public:
    int getClass2Int()
    {
        return _class2._myInt;
    }

    void setClass2Int(int val)
    {
        _class2._myInt = val;
    }

private:
    Class2 _class2;
};

int main()
{
    printf("Hello World\n");

    Class1 class1;

    printf("class1.getClass2Int() = %i\n", class1.getClass2Int());
    class1.setClass2Int(700);
    printf("class1.getClass2Int() = %i\n", class1.getClass2Int());

    return 0;
}

Output:

Hello World
class1.getClass2Int() = 123
class1.getClass2Int() = 700

References:

  1. https://www.geeksforgeeks.org/friend-class-function-cpp/
  2. Google search for "do you need to include header file to declare friend class?"
  3. Including header file in class that is a friend
like image 31
Gabriel Staples Avatar answered Sep 29 '22 04:09

Gabriel Staples