Friend functions and static data members
The answer was quite simple:
N4296::3.4.1/8 [basic.lookup.unqual]
:
For the members of a class X, a name used in a member function
body, in a default argument, in an exceptionspecification, in the
brace-or-equal-initializer of a non-static data member (9.2), or in
the definition of a class member outside of the definition of X,
following the member’s declarator-id31, shall be declared in one of
the following ways:[...]
(8.2) — shall be a member of class X or be a member of a base class of
X (10.2),[...]
N4296::3.4.1/9 [basic.lookup.unqual]
:
Name lookup for a name used in the definition of a friend function
(11.3) defined inline in the class granting friendship shall proceed
as described for lookup in member function definitions.
That's it.
UPD:
Inlining is important here. That's why the friend function defined outside the class defintion cannot use static members of the class directly. For instance, the following code pritns compile-time error:
#include <iostream>
class A
{
static int a;
friend void foo();
};
int A::a = 10;
void foo(){ std::cout << a << std::endl; }
int main(){ foo(); }
DEMO
What is different between static member function and global function?
Static class functions can
- access private and protected static data members within a class.
- access private and protected static functions.
- access private and protected per-instance data members within a class if the static function has an instance of the class.
- access private and protected per-instance functions if the static function has an instance of the class.
- shadow functions of the same name in base classes.
- access protected data and functions in base classes.
Global non-friend functions can do none of those.
What kind of member access do friend and static member functions have?
Remember that although prototypes for friend functions appear in a class definition, friend functions are not function members. A friend function of a class is defined away from the class, but a friend function has got access to non public members. Some people think "friendship" corrupts information hiding. Sometimes friend functions are used to make tester classes.
Static data members are used when an object class should to share an only one copy of a variable. So, you must use static data members for saving storage when just one copy is enough for all class members.
For static member functions you can look the following: When to use static member function?
Declare static functions as friend function?
Sort of linked: Is it possible to declare a friend function as static?
Personally I find the whole friend
thing a bit of a hack that breaks encapsulation but you've asked a valid question and the answer is that you can achieve what you want with a helper class:
file1.h
class MyClass {
private:
double someval;
friend class MyClassHelper;
};
file1.cpp
#include "file1.h"
struct MyClassHelper {
static void mutateMyClass(MyClass& ref) {
ref.someval=42;
}
};
// in "file1.cpp"
static void foo(MyClass &ref) {
MyClassHelper::mutateMyClass(ref);
}
Are you really sure you want to do it like this? Are you sure you don't want to encapsulate MyClass's mutators inside MyClass itself?
Cpp Friend function has no access to private static members
It because friend function main
in ClassA is located in nameA
namespace.
If you want to declare as friend the int main(void)
function, that located in global scope, you should do it this way:
friend int ::main(void);
Whole source (compiled in VS2015):
int main(void);
namespace nameA {
class ClassA {
private:
static int varA;
public:
ClassA() {};
friend int ::main(void);
};
}
namespace nameA {
int ClassA::varA = 0;
}
int main(void) {
nameA::ClassA::varA = 42;
return 0;
}
Friend class with only static member functions as a friend module in c++?
namespace is the currently relevant feature. But isn't possible to get a friend namespace, no more that it is possible to template a namespace or pass a namespace as a template parameters, two common cases where struct with only static members are commonly used as a work around. So there is a good precedent of using a struct. There is at least another reason to use a struct of static members instead of a namespace: a namespace is open to addition while a struct is closed. And that aspect alone could be also a justification of using a struct instead of a namespace for friend.
Declaring C++ static member functions as friends of the class in which it resides (syntax)
Actually, no need to use friend if it is static is more accurate. A static member function has access to the internals of the class just like a normal member function. The only difference is it doesn't have a this pointer.
void MyClass::Callback(void* thisptr) {
MyClass* p = static_cast<MyClass*>(thisptr);
p->public_func(); // legal
p->private_func(); // legal
p->private_int_var = 0; // legal
}
private static member function vs private member function
When you have a static
member variable, you choose its access level in just the same way that you would for a non-static
member variable. There's nothing "special" here.
Most of my private static
s tend to be things like built-in constants that are only used by the internals of the class.
I admit I can't think of many other use cases for them, but I will also tend to make any function static
if it logically has nothing to do with a particular instance of the class (and thus needs no non-static member access) — this may be a bit more OCD than some people indulge in though.
why can't i keep my design open by making it a private Non-Static member function even if it doesn't need access to private members(just like static member functions)
You can. It is up to you.
This way even if in future i require to access some private members i save myself from converting static to non-static mem func
Sure. I mean, it's one keyword. But this "forward-compatibility" may be useful if you need to keep your headers from changing (e.g. you're deploying them). Arguably this is a downside of making static
members private, where there aren't really many solid upsides. Again, it's up to you.
Related Topics
Finding Nearest Point in an Efficient Way
Training Custom Svm to Use with Hogdescriptor in Opencv
Request for Member '...' Is Ambiguous in G++
Order of Calling Base Class Constructor from Derived Class Initialization List
Counting the Number of Occurrences of a String Within a String
Std::Vector of Std::Vectors Contiguity
C++ Abstract Class Without Pure Virtual Functions
How to Use Setenv() to Export a Variable in C++
Thread Safety of Std::Map for Read-Only Operations
The C 'Clock()' Function Just Returns a Zero
What the Heque Is Going on with the Memory Overhead of Std::Deque
How Is Heap and Stack Memories Managed, Implemented, Allocated
Wrote to a File Using Std::Wofstream. the File Remained Empty
How to Extract the Mantissa of a Double
Address-Of Operator (&) VS Reference Operator(&)
New (Std::Nothrow) VS. New Within a Try/Catch Block
Why Does Wide File-Stream in C++ Narrow Written Data by Default