Inheritance and friend classes

HI all, I’m new to c++ and ue4 and I was just wondering if someone could please explain to me what is and when I would use a friend class in terms of c++ and ue4. Also a way I could use inheritance to communicate between classes in terms of c++ and ue4. Any help is much appreciated, thank you.

Hi,
I don’t have experience with friend classes.
Inheritance you are probably using already: when you create a C++
class in the engine you choose AActor for example as the parent.
So the MyActor you create then inherits from AActor and is a child.

You can then inherit from MyActor and give additional functionalities to the child class
and inherit basic functions/variables from MyActor.

I hope that helps, Peter

The easy way to remember about friends in C++ is friends can get at your private parts. :slight_smile:

3 Likes

Thank so much for your help, I understand the whole concept a lot better now. Is there any way I could have some examples of code using the MyActor you talked about and it’s inherited child class so I can know how to communicate between them. If not that’s totally fine, thank you anyways :slight_smile:

Ahh thank you :smiley:

Some times I use friend classes when building editor tools, to access private variables that nobody else should see.
​​​​​​

Okay, so you create a class called “Item” based on Actor

In code of your .h file will look like this:



UCLASS()
class TESTINGGROUNDS_API AItem : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
AItem();
}


this line


class TESTINGGROUNDS_API AItem : public AActor

means you are deriving from AActor

Then you create another class called “PickupHealer” based on “Item”:

The generated code will look like this:


UCLASS()
class TESTINGGROUNDS_API APickupHealer : public AItem
{
GENERATED_BODY()

public:

/** Constructor */
APickupHealer();
}

The line


class TESTINGGROUNDS_API APickupHealer : public AItem

shows that now this class is a child /deriving from “AItem”

AItem is now a child of AActor and will inherit any functions and variables.
APickupHealer is now a child of AItem and will inherit any variables and functions from AItem that you define in AItem. But it will also inherit from AActor since AItem is a child of AActor.

I hope this makes it clearer.

Best regards, Peter

2 Likes

Thanks, these examples really helped :slight_smile: