There is a subtle difference between implicit and explicit calling functions. Consider the following test-case out of the Standard doc
struct A { };
void operator + (A, A);
struct B {
void operator + (B);
void f ();
};
A a;
void B::f() {
operator+ (a,a); // ERROR – global operator hidden by member
a + a; // OK – calls global operator+
}
The first one fails because you gave a function name explicitly, and the name will be looked up "inside out": first in the function, then in its class, and then at global scope.
In the second one, lookup for function candidates works different: Member and non-member functions are looked up in two phases, and when looking up Non-member functions, member functions are ignored, so the member operator won't hide the global operator in the second case.
Explicit function calls will also never select a built-in operator. You cannot do operator+(10, 12);
for example. In general, always prefer implicit function calls, i would say.
Are all functions inside of a class member functions? Or only the ones preceded by the declaration "friend"?
As someone else said too, friend functions are not members of the class that contains the declaration, even if the functions is defined in the declaration too
struct A {
friend void f() {
std::cout << "member of the global namespace" << std::endl;
}
void g() {
std::cout << "member of class A" << std::endl;
}
};
The lexical scope of f
is A
- that means you can refer to (static) members or nested types of A
without preceding their names with A::
.
With regard to special member functions, you can call a destructor or assignment operator explicitly, while constructors cannot be called explicitly. Their calls are implicit, and is part of creating a new object.