What Is An Extended Friend Declaration in Modern C++?
In modern C++ the standards have a wonderfully named extended friend declaration feature to address the class declarations which is a template parameter as a friend. This extended friend declaration is available with the C++ compiler that has C++11 and above standards. In this post, we explain a friend and extended friend declaration in modern C++.
What is a friend declaration in modern C++?
The friend declaration is used in a class body with the friend
keyword and grants a function or another class access to private and protected members of the class where the friend declaration appears.
C++11 extends the current language to support a wider range of friend declarations. Now, in modern C++has the extended friend declaration feature to address class declarations which is a template parameter as a friend. This extended friend declaration is available with a C++ compiler that has C++11 and above standards.
Syntaxes for the friend declaration (since C++11):
friend typename_specifier ;
|
and
friend simple_type_specifier ;
|
Is there a simple example of a friend declaration in modern C++??
Here is a simple friend declaration C++ example.
class Ta;
class Tx { friend Ta; };
|
How do I make a function declaration with friend declaration in modern C++?
In C++, we can declare functions with friend
. Here is the syntax:
friend function_declaration
|
and we can declare the F()
function with friend
as shown below.
float F(float m) { return m*9.81; // F=m.g }
class Tx { friend float F(float m); };
|
How to create a function definition with friend declaration in modern C++?
In C++, we can define functions with friend
. Here is the syntax how we can do,
friend function_definition
|
and we can declare a reset()
function with friend
as below.
class Tx { int i; friend void reset( Tx& T) { T.i = 0; } };
|
What is an elaborated class specifier with friend declaration in modern C++?
According to the C++ standard, an elaborated type specifier must be used in a friend declaration for a class. That means the class-key of the elaborated_ type_specifier is required. In C++, we can use elaborated class specifier with friend
. Here is the syntax.
friend elaborated_class_specifier ;
|
How to use a template with friend declaration in modern C++?
Here is an template example that uses friend.
template <class Tx> class Tw { friend Tw; };
|
How to use a namespace with friend declaration in a class in modern C++?
Here is a friend example that is used in a class of a namespace.
class Ts;
namespace Ns { class Tx { friend Ts; }; }
|
Is there a full example of an extended friend declaration in modern C++?
Here is a full example to extended friend declaration in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include
class Ta;
class Tb { friend Ta; };
//———————————— class Tx { friend class Ta; };
//———————————— template <class Tz> class Ty { friend Ty; };
//———————————— class Ts;
namespace Ns { class Tx { friend Ts; }; }
//———————————— float F(float m) { return m*9.81; // F=m.g }
class Z { friend float F(float m); };
//———————————— class Txx { int i; friend void reset( Txx& T) { T.i = 0; } };
//———————————— template <class Tx> class Tw { friend Tw; }; //———————————— int main() { class Tb b1; class Tx x1;
class Z z1; class Txx xx1;
Ns::Tx o1;
return 0; }
|
For more information about the extended friend declarations feature, see Extended friend declarations Proposal document.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.