I have 2 classes (firstClass and secondClass) which firstClass is a friend of secondClass, and has a private nested unordered_map, which I want to access it in a function of secondClass.
So basically the code is like this:
class secondClass;
typedef unordered_map STable<unsigned, unordered_map<unsigned, double> > NESTED_MAP;
class firstClass{
friend class secondClass;
void myfunc1(secondClass* sc){
sc->myfunc2(&STable);
}
private:
NESTED_MAP STable;
};
class secondClass{
public:
void myfunc2(NESTED_MAP* st){
//Here I want to insert some elements in STable.
//Something like:
st[1][2]=0.5;
}
};
int main(){
firstClass fco;
secondClass sco;
fco.myfunc1(&sco);
return 0;
}
I know that it should be trivial, but I don't know how to solve it.
Any idea? (I changed the code and the question to make it more clear)
A friend class is allowed to access any private member, so you can simply invoke methods and modify properties as you would do if they had been public.
Here the documentation, it says:
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.
That said, by looking at your example, I'd rather change the place where to put the friend keyword, for it looks to me that myfunc2 ought not to be public.
It follows an example where I applied the above suggestion and that shows how to deal with private members from a friend class:
#include<unordered_map>
using namespace std;
class firstClass;
class secondClass{
friend class firstClass;
private:
void myfunc2(unordered_map<unsigned,double>& map){
map[1]=0.5;
}
};
class firstClass{
public:
void myfunc1(secondClass* sc){
// here firstClass is accessing a private member
// of secondClass, for it's allowed to do that
// being a friend
sc->myfunc2(STable);
}
private:
unordered_map<unsigned,double> STable;
};
int main(){
firstClass fco;
secondClass sco;
fco.myfunc1(&sco);
return 0;
}
Related
I have two classes:
class CALLDB;
class CALL
{
friend class CALLDB;
public:
string GetStart()const;
private:
string start;
};
And second class:
class CALLDB
{
friend class CALL;
public:
unsigned int Load(istream& fin);
private:
unsigned int numCalls;
};
In the main function, I did this:
int main(){
CALLDB calldata;
cout<<calldata.numCalls;
}
And then it says:
error C2248: 'CALLDB::numCalls': cannot access private member declared in class 'CALLDB'
Why does it happen? Is something wrong with my friend class declaration?
friend class and functions are allowed to access the private data members of a class. so inside the class which is made as friend to another class,you can access the private member.to access the private members inside the friend class or friend function we have to create object for that class and then only we can access the private members.
in your example you made CALLDB as friend to CALL and CALL to CALLDB so both the classes can access the private members of other class (i.e) you can access the private member of CALLDB in CALL and private member of CALL in CALLDB.but you tried to access the private members from main function which is not a friend to a class.I hope that you understand the concept.here i have given an simple example to understand the concept of friend class .you try that and do your program as per your requirement
#include <iostream>
Using namespace std;
class CALLDB;
class CALL
{
friend class CALLDB;
private:
void display()
{
cout<<"\n from Private function of display() of the class CALL ";
}
};
class CALLDB
{
friend class CALL;
public:
void output()
{
CALL ca;
cout<<"\n from public function output of CALLDB class ";
cout<<"\n Calling of private function display of class CALL";
ca.display();
}
};
int main()
{
CALLDB cd1;
cd1.output();
}
OUTPUT
from public function output of CALLDB class
Calling of private function display of class CALL
from Private function of display() of the class CALL
consider the following example.
class A
{
int member;
};
class B
{
A& a_ref;
void manipulate()
{
a_ref.member++;
}
};
Now, obviously, B::manipulate can not access a_ref. I would like to allow (only) class B to get (reference) to A::member. I know that there exists friend keyword, but I do not know how to use it properly. My intention is, that I could change B::manipulate implementation to become this
int& A::only_B_can_call_this() // become friend of B somehow
{
return member;
}
void B::manipulate()
{
a_ref.only_B_can_call_this()++;
}
To make B a friend:
class A
{
int member;
friend /*class*/ B; // class is optional, required if B isn't declared yet
};
Note that friends are anti-patterns - if something is private, it probably shouldn't be accessed. What are you trying to accomplish? Why isn't A self-contained? Why does another class need to access its internal data?
Use friend if you have valid answers/reasons for these questions.
You simply add friend class <class_name>; to the class that wants to make its members accessible, where <class_name> is the name of the class that you want to provide access for.
class A
{
friend class B; // allow class B to access private members of A
int member;
};
class B
{
A& a_ref;
void manipulate()
{
a_ref.member++;
}
};
this is my first question in here :)
i have i little problem..
these are my classes:
class Gracz{
char znak_gracza;
public:
Gracz();
Gracz(char znak){
this->znak_gracza = znak;
};
friend void multiplayer();
};
class Osoba: public Gracz{
public:
Osoba();
Osoba(char znak){
this->znak_gracza = znak;
};
friend void multiplayer();
};
i also have a function multiplayer, where i try tu use constructor with argument:
void multiplayer(){
Osoba gracz1('O');
Osoba gracz2('X');
...
}
but it doesn't work.
errors are same for gracz1 and gracz2
error C2248: 'Gracz::znak_gracza' : cannot access private member declared in class 'Gracz'
see declaration of 'Gracz::znak_gracza'
see declaration of 'Gracz'
Derived classes cannot access private members of a parent class. You can declare them as protected (which is like private but lets derived classes access it), but in your case, since Gracz provides a way to initialize the variable, you should just let Osoba pass the argument to Gracz constructor.
Osoba(char znak)
: Gracz(znak) // initializes parent class
{}
private member access is only available to members of the class, and friends. what you're looking for it to declare char znak_gracza as protected, so classes that inherit Gracz have access to that member as well.
your class Gracz should look more like this:
class Gracz{
protected:
char znak_gracza;
public:
Gracz();
Gracz(char znak){
this->znak_gracza = znak;
};
friend void multiplayer();
};
The constructor needs to pass the parameter to the base class constructor:
class Osoba: public Gracz{
public:
//...
Osoba(char znak) :
Gracz(znak) {
}
};
The multiplayer function is a friend of the Gracz class, but the Osoba class isn't.
Remember that child classes can't automatically access parent classes private variables. If you want Osoba to access the znak_gracza variable you have to make it protected.
I'm trying to make the following bit of code to work...
#include <list>
template <typename T>
class container{
public:
virtual T func_x(){
T temp;
//do stuff with list<t> test
return temp;
}
private:
std::list<T> test;
};
template <typename T>
class container2 : public container<T>{
public:
virtual T func_x(){
T temp;
//do different stuff with list<T> test
return temp;
}
};
What I want to be able to do is declare
container<T> x;
container2<T> y;
And be able to have y have access to all the public functions of x, except that it behaves differently for func_x.
The problem I have now is that func_x in class container2 cannot use;
std::list<T> test;
I have even tried making class container completely public. Still no dice. Can this be done?
Thanks!
Members are by default private for classes:
template <typename T>
class container2 : public container<T>{
//************
// no modifier
virtual T func_x(){
T temp;
//do different stuff with list<T> test
return temp;
}
private:
std::list<T> test;
};
means that func_x is private, since no modifier is specified.
You need to explicitly declare func_x as public, as you have for class container.
"Just because it's public in the base class doesn't mean it's automatically that way for the derived".
EDIT:
If you want base class members to be accessible in derived classes, you must declare them either protected or public. So, to answer your follow-up question, change
private:
std::list<T> test;
to
protected:
std::list<T> test;
Also, in the future, don't edit the question to ask a new one. You should create a new question to deal with the new issue. It can be misleading for others who see answers that no longer apply to the new question.
You need to add public: to your class declaration: otherwise, all members declared are private by default.
template <typename T>
class container2 : public container<T>{
public: // <<==== ADD THIS
virtual T func_x(){
T temp;
//do different stuff with list<T> test
return temp;
}
private:
std::list<T> test;
};
The problem is that your func_x is hidden by your derived object, because you've redefined it, as private in derived.
You need to make it public.
When defining a class as a friend class, does it matter in which accessor section the definitions is placed, and if so does that change the members the friend has access to?
class aclass
{
private:
// friend bclass;
public:
// friend bclass;
protected:
// friend bclass;
};
class bclass
{};
Access specifiers do not apply to friend function/Class
You can declare the Friend Function or Class under any Access Specifier and the Function/Class will still have access to all the member variables(Public,Protected & Private) of that Class.
Once you place friend class/function inside a given class (say 'aclass') anywhere. It will have access to all defined members of the class (irrespective of public/private/protected); for example:
class aClass
{
public: int pub; void fun1() {}
protected: int pro; void fun2() {}
private: int pri; aClass(const aClass& o);
friend void outsider ();
};
Friend function outsider() can access pub, pro, pri, fun1, fun2; but not aClass copy constructor in this case (if it's not defined anywhere).
Friend functions aren't placed inside any accessors by convention, because by definition they aren't part of the class. You might do something like this:
class Elephants
{
//friend void notAMemberFuncion(argument 123);
public:
// member functions;
protected:
// data members;
};
The friend class/function can access all the private/protected/public members of class, which access section the friend class/function is placed doesn't make any difference.
It's suggested to put friend class/function in the public section, because friends is part of the class interface.