Cpp Friend function has no access to private static members - c++

I have a class with a private static variable. The main function should change the value in the variable, but even if I set the main function as a friend of the class the compiler tells me that the variable is private and not accessible from main.
Example:
ClassA.h:
namespace nameA{
class ClassA {
private:
static int varA;
public:
ClassA(){};
friend int main(void);
};
}
ClassA.cpp:
namespace nameA{
int ClassA::varA = 0;
}
main:
int main(void){
ClassA::varA = 42; //ERROR
}
I don't know if "friend" also allows access to static members or if I have to find another solution.

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;
}

Your friend declarations grants friendship to a function named main in the namespace nameA, not the global main function.
Your code is equivalent to
namespace nameA
{
int main(void);
class classA
{
...
friend int main(void);
};
}
You need to declare main before the namespace starts.
int main(void);
namespace nameA
{
class classA
{
...
friend int main(void);
};
}
should work

Related

How do I make a function of a namespace inaccessible by the client side

I'm writing a class which is wrapped in a namespace named cs. I am using a library whose one of the function takes a function pointer. The function have to modify some of the protected members of the class so I wrote a free function in the same cs namespace and made it a friend function of the class. Doing that made it available for the clients to use that function. But The function MUST be inaccessible from the client due to obvious reasons.
An example code is here:
#include "lib.h"
namespace cs{
class A
{
protected:
int x;
float y;
friend int myFunc(void* userdata, int valInt, float valFloat);
public:
void abc()
{
libFunc(this, myFunc);
}
};
void myFunc(void *userdata, int x, float y){
// I would like this function to be inaccessible from the client
A *obj = (A*) userdata;
obj->x = x;
obj->y = y;
}
}
If you want to make a free function inaccessible in another compilation unit, you may use a nested anonymous namespace:
namespace cs{
class A
{
protected:
//...
friend int myFunc(int valInt, float valFloat);
public:
void abc();
};
namespace { // anonymous nested namespace
int myFunc(int x, float y){
...
}
}
void A:: abc() {
libFunc(this, myFunc);
}
}

How to fix the Error in static member that is private?

Why i is being called private?as i is static member ,then it must not an error in line no.19
#include<iostream>
using namespace std;
class myClass{
static int i;
public:
void seti(int a)
{
i=a;
}
int geti()
{
return i;
}
};
int myClass::i;
int main()
{
myClass ob1,ob2;
cout<<myClass::i<<endl;
ob1.seti(200);
cout<<ob1.geti()<<endl;
cout<<ob2.geti()<<endl;
}
Here the issue is that member attributes of a class are private by default. That means that i is private and that you can't access it from outside your class.
One solution would be to declare i as a public member.
class myClass{
public:
static int i;
void seti(int a)
{
i=a;
}
int geti()
{
return i;
}
};
PS : members of a struct are public by default

Access private variable of class defined in header from class defined in source file

Header.h
#pragma once
namespace
{
class B;
}
namespace n1
{
namespace n2
{
class A
{
private:
int i;
public:
friend class B;
};
}
}
Source.cpp
#include <stdio.h>
#include "Header.h"
class B
{
public:
void Run();
};
void B::Run()
{
n1::n2::A a;
a.i;
}
int main()
{
B b;
b.Run();
}
As we can see from above Class A is defined in header file while class B is defined in source file. I want to access private member of Class A from Class B::run(). I am not able to find the way to do this.
you are forward declaring class B in anonymous namespace
take out class B forward declaration out of the namespace and it should work
like this:
#pragma once
class B;
namespace n1
{
namespace n2
{
class A
{
private:
int i;
public:
friend class B;
};
}
}

friendship and namespace issue

I'm having troubles with friendship between a class of a namespace and a function as below:
How to tell that the friend function is outside of the namespace?
Thanks
namespace NS
{
class Class
{
public:
Class();
virtual ~Class();
private:
void Foo();
friend void Bar(Class&);
};
}
void Bar(NS::Class& c)
{
c.Foo();
}
By using the scope operator ::
friend void ::Bar(Class&);
This tells the compiler that Bar is in the global scope.
Apparently the Bar function needs to be declared before it's used in the friend declaration when using the scope operator. The problem is that for Bar to be declared you need to declare both the namespace NS and the class NS::Class.
Something like this
namespace NS
{
class Class;
}
extern "C"
{
void Bar(NS::Class& c);
}
namespace NS
{
class Class
{
public:
Class();
virtual ~Class();
private:
void Foo() {}
friend void ::Bar(Class&);
};
}
void Bar(NS::Class& c)
{
c.Foo();
}

Friend function cannot access private function if class is under a namespace

I have a class inside a namespace and that class contains a private function. And there is a global function. I want that global function to be the friend of my class which is inside the namespace. But when I make it as a friend, the compiler thinks that the function is not global and it is inside that namespace itself. So if I try to access the private member function with global function, it doesn't work, whereas if I define a function with the same name in that namespace itself it works. Below is the code you can see.
#include <iostream>
#include <conio.h>
namespace Nayan
{
class CA
{
private:
static void funCA();
friend void fun();
};
void CA::funCA()
{
std::cout<<"CA::funCA"<<std::endl;
}
void fun()
{
Nayan::CA::funCA();
}
}
void fun()
{
//Nayan::CA::funCA(); //Can't access private member
}
int main()
{
Nayan::fun();
_getch();
return 0;
}
I also tried to make friend as
friend void ::fun();
And it also doesn't help.
You need to use the global scope operator ::.
void fun();
namespace Nayan
{
class CA
{
private:
static void funCA();
friend void fun();
friend void ::fun();
};
void CA::funCA()
{
std::cout<<"CA::funCA"<<std::endl;
}
void fun()
{
Nayan::CA::funCA();
}
}
void fun()
{
Nayan::CA::funCA(); //Can access private member
}
The fun() function is in the global namespace. You need a prototype:
void fun();
namespace Nayan
{
class CA
{
private:
static void funCA() {}
friend void ::fun();
};
}
void fun()
{
Nayan::CA::funCA();
}