Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a class-key must be declared when declaring a friend

Tags:

The g++ compiler complains with this error when I declare a friend thusly:

friend MyClass; 

instead of

friend class MyClass; 

Why should the class keyword be required? (the Borland C++ compiler, BTW, does not require it.)

Couldn't the compiler simply look-up MyClass in the symbol table and tell it was declared as a class? (it is obviously doing the look-up anyway because it complains when MyClass it not declared)

It is not like it is making a forward declaration of the class: I still have to have either declared the class above or at least have forward declared it.

It would make sense to me (would be great actually) if

friend class MyClass; 

makes a forward declaration if needed, otherwise it just seems like syntactic salt to me.

I have been merrily using friend statements without the class or struct keyword with no compiler complaints for almost 20 years. Is this something fairly new?

like image 731
Roger Nelson Avatar asked Mar 18 '09 04:03

Roger Nelson


People also ask

Can class be a friend in c++?

Friend Keyword in C++ But, to declare any class as a friend class, you do it with the friend keyword. You can use the friend keyword to any class to declare it as a friend class. This keyword enables any class to access private and protected members of other classes and functions.

How can we declare a class a friend of another class in C++?

A function or class can't declare itself as a friend of any class. In a class definition, use the friend keyword and the name of a non-member function or other class to grant it access to the private and protected members of your class. In a template definition, a type parameter can be declared as a friend .

Can you declare an entire class as a friend of another class?

You can declare an entire class as a friend. Suppose class F is a friend of class A . This means that every member function and static data member definition of class F has access to class A .

How we can make some specific member functions of one class friendly to another class?

We can also make a function of one class as a friend of another class. We do this in the same way as we make a function as a friend of a class. The only difference is that we need to write class_name :: in the declaration before the name of that function in the class whose friend it is being declared.


1 Answers

I was surprised about this (and as a result deleted a previous incorrect answer). The C++03 standard says in 11.4:

An elaborated-type-specifier shall be used in a friend declaration for a class.

Then to make sure there's no misunderstanding, it footnotes that with:

The class-key of the elaborated-type-specifier is required.

GCC is the only compiler that I have that complains about the missing class-key, but it looks like other compilers are letting us get away with something non-standard...

Now as for the rationale - you'd have to ask someone who knows more about compilers (or standards) than I do.

like image 78
Michael Burr Avatar answered Sep 28 '22 12:09

Michael Burr