Notification texts go here Contact Us Buy Now!

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

In C++, a type alias is not allowed to be a friend class name because the syntax for befriending a type alias is different from that of befriending a class.

To befriend a type alias, you use the following syntax:

friend C;

where C is the type alias.

On the other hand, to befriend a class, you use the following syntax:

friend class C;

where C is the class name.

The reason for this difference in syntax is that a type alias is not a class. It is simply an alias for another type. Therefore, it does not have the same properties as a class, and it cannot be befriended in the same way.

If you try to befriend a type alias using the syntax for befriending a class, you will get a compiler error. The compiler will tell you that the type alias is not a class and cannot be befriended.

To fix this error, you need to use the correct syntax for befriending a type alias. Here is an example:

class B {
public:
  friend C; // Befriend the type alias C

private:
  int x;
};

typedef int C; // Define the type alias C

In this example, the type alias C is befriended by the class B. This means that the class B can access the private members of the type alias C.

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.