Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "friend"ing a class extend to classes declared within that class?

Tags:

c++

friend

I have the following code where class A declares class B as friend. Should class C, declared within class B, be able to view private declarations/members of class A?

It compiles without error with CL version 16 (Visual Studio 2010), but gcc g++ version 4.1.1 gives the error "typedef int A::T is private within this context".

The same behaviour occurs with functions calls as typedefs (which is how I discovered the difference).

class A {
   friend class B;
   typedef int T;
};

class B {
   A::T t; // ok
   typedef A::T U; // ok
   class C {
      U u; // ok
      A::T v; // compile error on gcc
   };
};

I have seearched briefly, but not been able to find the right search terms. I've yet to read through the standard. Are there any previous questions on the subject, or mentioned in the C++ FAQ? Which behaviour is imlpied by the standard, if either?

like image 900
Jack V. Avatar asked May 12 '11 08:05

Jack V.


People also ask

What happens when a class is declared as friend of another class?

Friend Function in C++ Declaring a function as friend grants it a special ability to access private and protected data members of a class. You can declare a friend function as a member of another class or a global function.

Can friend function declare outside the class?

The friend function can be a member of another class or a function that is outside the scope of the class. A friend function can be declared in the private or public part of a class without changing its meaning.

Can friend function be defined inside class?

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

Does friend need forward declaration?

Friend Functions For a free function, it is very straightforward and a forward declaration is not required. We can simply declare the friend as follows: The void Print(const Test& test) function has access to the private members of the Test class.

When a function is declared a friend by a class?

A friend function in C++ is a function that is declared outside a class but is capable of accessing the private and protected members of the class. There could be situations in programming wherein we want two classes to share their members. These members may be data members, class functions or function templates.

Can a function be friend of two classes?

A friend function can be friendly to 2 or more classes. The friend function does not belong to any class, so it can be used to access private data of two or more classes as in the following example. The friend functions can serve, for example, to conduct operations between two different classes.


1 Answers

From standard docs., $11.4.2

Declaring a class to be a friend implies that the names of private and protected members from the class granting friendship can be accessed in the base-specifier s and member declarations of the befriended class.

An example from the standard docs., themselves,

class A {
class B { };
friend class X;
};
struct X : A::B { // OK: A::B accessible to friend
    A::B mx; // OK: A::B accessible to member of friend
    class Y {
        A::B my; // OK: A::B accessible to nested member of friend
    };
};

Hence it should work without any error.

like image 61
liaK Avatar answered Oct 06 '22 00:10

liaK