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();
}
};
Related
#include <iostream>
#include<string>
using namespace std;
class Human
{
private:
string Name;
int Age;
friend class Utility;
public:
Human(string InputName,int InputAge)
{
Name = InputName;
Age = InputAge;
}
};
class Utility
{
public:
void DisplayAge(const Human& Person)
{
cout<<Person.Age<<endl;
}
};
int main()
{
Human FirstMan("Adam",25);
cout<<"Accessing private member Age via friend class: ";
Utility::DisplayAge(FirstMan);
}
I don't understand..when I call the function I do send an object(FistMan)..why my compiler still says that I call it without object?
DisplayAge in Utility is not a static function. Therefore you need an instance of Uitility in order to call it.
So, either make the function static, or call it via an anonymous temporary
Utility().DisplayAge(FirstMan);
Better still, make DisplayAge a member function of Human.
Use the static keyword and then you'll be able to call your function on your class
I edited your code below :
#include <iostream>
#include<string>
using namespace std;
class Human
{
private:
string Name;
int Age;
friend class Utility;
public:
Human(string InputName,int InputAge)
{
Name = InputName;
Age = InputAge;
}
};
class Utility
{
friend class Human;
public:
Utility() = default;
static void DisplayAge(const Human& Person)
{
cout<<Person.Age<<endl;
}
};
int main(void)
{
Human FirstMan("Adam",25);
cout<<"Accessing private member Age via friend class: ";
Utility::DisplayAge(FirstMan);
}
Dön't use a class whenever you want to define functions. Use a namespace:
namespace Utility
{
inline void DisplayAge(const Human& Person)
{
cout<<Person.Age<<endl;
}
}
int main()
{
Human FirstMan("Adam",25);
cout<<"Accessing private member Age via friend class: ";
Utility::DisplayAge(FirstMan);
}
I've been trying to write up a code to implement a member function that could access private data of a class by declaring it as a friend within the class. But my code is failing and I can't seem to figure out what's wrong with it:
#include <iostream>
using namespace std;
class A;
class B
{
private:
int b; // This is to be accessed by member function of A
public:
friend void A::accessB();
};
class A
{
private:
int a;
public:
void accessB();
};
void A::accessB()
{
B y;
y.b = 100;
cout << "Done" << endl;
}
int main(void)
{
A x;
x.accessB();
}
I am trying to access the private contents of B making use of getAccessB function which is member function of A. I have declared it as a friend. What's wrong with this?
A::accessB is not declared at the point of the friend declaration, so you can't refer to it. You can fix this by shifting the order of definitions and declarations such that A::accessB is visible to B:
class A
{
private:
int a;
public:
void accessB();
};
class B
{
private:
int b;
public:
friend void A::accessB();
};
void A::accessB()
{
B y;
y.b = 100;
cout << "Done" << endl;
}
Note that making a function a friend just so that it can modify your private state is a bad code smell. Hopefully you are just doing this to understand the concepts.
The ordering. If you are referring to A::accessB, A must be declared by that point. Place the A class above the B, and it will work fine.
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);
}
#include <iostream>
#include <cstring>
using namespace std;
class Obj;
class Test {
friend class Obj;
public:
Test()
{
}
~Test()
{
}
void foo()
{
//print();
//Obj::print();
//Obj x;
//x.print();
}
};
class Obj {
public:
void print()
{
cout << "print here" << endl;
}
};
int main()
{
Test test;
test.foo();
return 0;
}
Quick question,how can I call print the correct way in Test::foo() ?
You need to define the member function after the definition of Obj:
class Test {
public:
void foo();
};
class Obj {
public:
void print() { }
};
void Test::foo() {
Obj o;
o.print();
}
As mentioned by james you should define the member function after the definition of Obj. Also you are calling Obj::print, but print is not a static member function so you must call it on an instance of Obj not Obj itself.
If you really do want print to be a static member, declare it so.
class Obj {
public:
static void print(){ blah }
}
Also you do not need to make Obj a friend in order to access its public methods.
Also can OP please define "correct way", I was assuming you wanted it to be a static member function, james' answer is correct if you want one instance of Obj per instance of Test.
UPDATED
OP, as per your comment you must have the declaration of Obj along with print BEFORE using it within Test. This can be achieved in many ways:
move the entire class Obj defintion (and declaration) before Test
declare Obj's methods with its class definition and define them later.
declare Test like you have and Define Test as per James' post (after Obj).
The following works fine:
#include <iostream>
#include <cstring>
using namespace std;
class Obj {
public:
static void print()
{
cout << "print here" << endl;
}
};
class Test {
public:
Test()
{
}
~Test()
{
}
void foo()
{
Obj::print();
}
};
int main()
{
Test test;
test.foo();
return 0;
}
However it is always nicer (in my opinion) to separate declaration from definition for all but the most trivial of cases.
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();
}