Friend function in c++ - c++

Why we always say that friend function are not member function even though they are declared in the class?
I have found in many books and on internet but i am not getting proper explanation.

friend (C++)
If you declare a friend function that was not previously declared,
that function is exported to the enclosing nonclass scope.

In [class.mfct] the C++ standard says (or said around the time of C++11. The link's bit out of date at this point)
Functions declared in the definition of a class, excluding those declared with a friend specifier ([class.friend]), are called member functions of that class.
I'm having difficulty finding similar wording in later drafts of the standard.
That said, I consider this one self-evident.
[class.friend] states:
A friend of a class is a function or class that is given permission to use the private and protected member names from the class. A class specifies its friends, if any, by way of friend declarations. Such declarations give special access rights to the friends, but they do not make the nominated friends members of the befriending class.
A friend is something outside the class that has been granted access by the class to the protected and private members of the class. This implies that a friend is not a member itself.
Note also that the friend function does not have to be implemented within the class. A declaration is sufficient.

Conceptually, a member function is of type Ret(Class::*)(Args...), is called on an instance of the class: instance.member_function(), and has access to the called instance via this. A friend function doesn't fit with "member functions". The function is defined in a separate scope and not in the class even though it looks similar to a member function declaration.

Related

Operator[] overloading using friend function

Why isn't it possible to overload the subscript operator (operator[]) as a friend function?
As Bjarne Stroustrup says in the D&E book:
However, even in the original design of C++, I restricted operators [], (), and -> to be members. It seemed a harmless restriction that eliminated the possibility of some obscure errors because these operators invariably depend on and typically modify the state of their left-hand operand. However, it is probably a case of unnecessary nannyism.
Friends are not extensions to your class, just a keyword saying xy has access to your class's private or protected members. You can not override or add functions this way.
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.
Source

What exactly is a "member" of a class in C++?

I am new to C++, and have experience in Java and Python. I tried to search this question on Stack for a while, but did not find any questions that resembled this (although maybe that is because of my cursory knowledge of C++).
I was reading the C++ Primer book until I stumbled upon "members" of classes in C++. I can understand the concept of a class from Java, but I am unsure of what a "member" is.
Is a member simply an instance of a class? If so, how come it seems like the variables in a class are also considered members (in the Primer, the ISBN number of a class for a book is considered a member)?
Could anyone give a general definition of a "member" in C++?
A member is some entity that belongs to a class.
If a class has a function, this is a member function - you might know it as "a method".
If a class has a variable, this is a member variable - you might know it as "a property".
int a;
void f () {};
class A{
int m_A;
void m_F(){}
}
a is a global variable.
f is a global function.
m_A is a member variable or "property" of the class A.
m_F is a member function or "method" of the class A.
A member is defined as the variables and functions within a class.
Variables defined within a class are sometimes referred to as member variables. Similarly functions can be called member functions. Besides this, there isn't much to it.
According to the C++ Standard (9.2 Class members)
1 The member-specification in a class definition declares the full set
of members of the class; no member can be added elsewhere. Members of
a class are data members, member functions (9.3), nested types, and
enumerators. Data members and member functions are static or
non-static; see 9.4. Nested types are classes (9.1, 9.7) and
enumerations (7.2) defined in the class, and arbitrary types declared
as members by use of a typedef declaration (7.1.3). The enumerators of
an unscoped enumeration (7.2) defined in the class are members of the
class. Except when used to declare friends (11.3) or to introduce the
name of a member of a base class into a derived class (7.3.3),
member-declarations declare members of the class, and each such
member-declaration shall declare at least one member name of the
class. A member shall not be declared twice in the
member-specification, except that a nested class or member class
template can be declared and then later defined, and except that an
enumeration can be introduced with an opaque-enum-declaration and
later redeclared with an enum-specifier.
Also class members are
using-declaration
static_assert-declaration
template-declaration
alias-declaration
I guess a google search would have worked cause I just searched for 5 seconds:
Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.
You can check a more complete definition here: http://www.cplusplus.com/doc/tutorial/classes/
In a nutshell both the data members (i.e variable of the class) and the functions are members of a class.
As far as I remember, java members are exactly the same.

C++: Difference Between Non-Member Function and Static Member Function?

Simple question, here: what is the difference between a static member function, i.e. a function that can be called without requiring an object to access it (simply using the class identifier), and a non-member function? Here, I am asking both conceptually and functionally.
Are non-member functions conceptually static?
static member functions can access private and protected sections of a class. Non-member functions cannot do that as default. They can do that only if a class grants them friendship.
Another point to consider is that name of the static member functions are in the scope of the class. Multiple classes can have static member functions with the same name without worrying about names conflicting.
I would like to append the answer of #R Sahu that overloaded operators may not be static functions of a class.:)
Also static functions themselves can be protected and private. So they can be inaccesible outside the class where they are declared or its derived classes.
The other advantage of a static member function is that it is the only way if you want to call it in a thread in Windows API. CreateThread requires a function either to be in the global space, or, if it is a member function it has to be static. And there is no way around it, at least to my knowledge.

C++ : friends of class and "this" pointer

I have one little question for you :), I understand that every method "secretly" gets "this" pointer of some class that they are inside but why that wont happen to "friend" functions ?
Is it because they are NOT methods of class?
Can anyone explain whole machinery, i am very interested in how "this" really works!
thanks in advance! :)
friend functions and classes are just used for access control checked by the compiler.
friend functions are just standard functions, so there won't be any differences regarding calling conventions.
friend functions are not member of any class, so no this pointer is passed (as done with static member functions)
Non-static member function of a class will get a hidden this pointer (depending on the ABI this is often the first argument), static member functions don't get the this pointer because they don't act on instance data.
How exactly the this pointer is passed to a member function heavily depends on the used ABI, which depends on the architecture and operating system. Either it will be pushed on the stack, or it will be passed through a well known register.
Please consider reading "Where is the 'this' pointer stored in computer memory?".
"Friendship" and "Membership" are two different things. A function can be a member function or not, and independantly be a friend function or not.
You can declare a member function to be a friend function of another class, i.e.
class B{
friend void A::func(B);
//stuff
};
Here, the member function func from class A is declared as friend and can access B's private, and it will have a this pointer, that points to the object of class A on which func has been called.
The this pointer is an implicit parameter of non-static member functions, which is described in section 9.3.2 of the C++ Standard. How it is passed to the function depends on your compiler/architecture, i.e. it is implementation defined (so you might want to read your favorite compiler's documentation to learn about how it manages this pointers).

Are Local class, Inner class and Nested class are the same things in C++?

Are Local class, Inner class and Nested class mean same things in C++?
Local Class and Nested class are different beasts.
A Nested class is a class declared within the scope of another class.
A Local class is declared within a function definition.
Inner class is a non standard C++ term, So I am not sure how to define it.
Nested Classes:
IBM Documentation as a nice documentation here.
To Summarize:
The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.
Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class
Local Classes:
This answer of mine here documents the subtle points associated with local classes.
Quoting draft of C++11 (N3290):
9.7 Nested class declarations [class.nest]
1 A class can be declared within another class. A class declared within another is called a
nested class.
9.8 Local class declarations [class.local]
1 A class can be declared within a function definition; such a class is called a local class.
There is no concept of inner class specified in C++ standard.
Inner classes capture the enclosing class's this reference. A hierarchy of inner class instances form a tree structure. This is unique for Java.