Notification texts go here Contact Us Buy Now!

Why is a type alias not allowed to be a friend class name in C++?

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.

class B { public: friend C; // Alias C is a friend of B };

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.

typedef int MyInt; friend MyInt; // This is the same as friend int;

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.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.