While it's true that type aliases can be used as friend class names in C++, there is a subtle difference in syntax that can lead to confusion.
The correct syntax for befriending an alias C
is friend C;
instead of friend class C;
.
When you write friend class C;
you're actually making a global class named C
a friend of B
. To make the alias a friend, you just need to remove the class
keyword.
This will allow the alias C
to access the private and protected members of class B
.
It's important to note that type aliases are not actual types in C++, they are simply another name for an existing type. This means that when you befriend an alias, you're actually befriending the underlying type.
In this example, befriending the alias MyInt
is the same as befriending the built-in type int
.
I hope this clarifies the issue. If you have any further questions, feel free to ask.