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();
}
Related
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);
}
}
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
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();
}
In main, I want to access the display function. Here, in class B I declared class A as friend. So i thought that it is possible to access the private member functions.
But i dont know how to do that.
#include<stdio.h>
class A
{
public:
class B
{
public:
friend class A;
private:
void display()
{
printf("\nHi");
}
};
};
int main()
{
//here i wanna access display function.. is it possible?
return 1;
}
friend specifies what has access to private members. In your case, you want to access private members in the main function, so you should specify that it's friend:
class A
{
public:
class B
{
friend int main();
void display()
{
printf("\nHi");
}
};
};
int main()
{
// here you can access display function:
A::B object;
object.display();
}
Alternatively, if you want to make class A (and not anything else) a friend, then class A should access the display function. Any member of class A can do it:
class A
{
public:
class B
{
friend class A;
void display()
{
printf("\nHi");
}
};
// here you can access display function:
void access_display(B object)
{
object.display();
}
};
int main()
{
A object1;
A::B object2;
object1.access_display(object2);
}
Hello I am trying to access a private member function is Gtest.
The code looks somewhat similar to this. So, how can I access static void Pri_fun?
using namespace std;
class test{
};
class abc{
public:
friend class test;
private:
static void Pri_fun()
{
cout << "private fun called \n";
}
};
int main()
{
abc ab;
test *abd;
abd->Pri_fun();
}
Since it's a static function, you should access it via the class name:
abc::Pri_fun();
You should make a caller function though, or call it from the friend class' constructor:
class test{
public:
void foo()
{
abc::Pri_fun();
}
};
or
class test{
public:
test()
{
abc::Pri_fun();
}
};