Cannot access private member declared in class, even declared friend class - c++

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

Related

How do I access private member variables with public member functions (derived class)?

I need to get access of the base class private member variables using public member functions from the derived class. That means, I can call it anytime in a public member function from the derived class and change it anytime.
For example:
# include <iostream>
using namespace std;
class A
{
private:
int a = 19;
public:
int getA() // Function to get A
{
return a;
}
};
class B : public A
{
public:
void getAatB() // How do I get a variable at parent class (A)?
{
cout<<getA();
}
};
int main()
{
B a;
a.getAatB();
return 0;
}
I need to get access to variable a. How do I do this without changing private to protected or public at class A?
the easy way of doing that is adding friend class B; to class A , so class B can access class A's attributes and you aren't going to need change private to protected.
If you want only the descendants of A having a access to getA() member function, there is a special keyword protected in c++. The purpose of protected, as stated here is.
Class members declared as protected can be used only by the following:
Member functions of the class that originally declared these members.
Friends of the class that originally declared these members.
Classes derived with public or protected access from the class that
originally declared these members.
Direct privately derived classes that also have private access to
protected members.

Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

class baseClass {
public:
friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; }
baseClass(int x) : baseInt(x) {}
private:
int baseInt;
};
class derivedClass : public baseClass {
public:
derivedClass(int x, int y) : baseClass(x), derivedInt(y) {}
private:
int derivedInt;
};
in the function friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; }
I don't understand why would the friend function of the base class work for the derived class?
should not passing derived class obj. instead of a base class obj. tconsidered as error?
my question is why it works when i pass a derived class object to it?
Are friend functions inherited?
No, friend functions are not inherited.
Why would a base class function work on a derived class object?
Because friend function is using the data members available in base class only. Not the data members of derived class. Since derived class is a type of base class So, friend function is working fine. But note that here derived class instance is sliced and having information available only for base class.
friend function will report an error if you will try to access restricted members of derived class. e.g.
int friendFuncReturn(baseClass &obj) { return ((derivedClass)obj).derivedInt; }
No. You cannot inherited friend function in C++. It is strictly one-one relationship between two classes.
C++ Standard, section 11.4/8
Friendship is neither inherited nor transitive.

C++ Accessing a private member in a friend class

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

cannot access private member declared in class

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.

Friend of a derived class can access what variables?

To simplify my problem, I have something like this:
class Base {
private:
protected:
int a,b;
string c;
public:
[some functions here]
}
class Derived : public Base{
[some variables and functions]
friend void function();
}
void function(){
int d[a][b];
[stuff]
}
basically, my void function needs to access stuff that is in the protected class of the base class. I'd like to keep those variables defined in the protected section. Is there anyway for function, which HAS to be friended into the Derived class, to be able to access a and b?
You can define private methods in Derived: any method in Derived can access the protected members of Base.
Function is a friend of Derived, so it can invoke those private methods in Derived, which in turn access the protected members of Base.
Edit to reply to the comment below
The get_a() member method and the a member data are members of their classes. Instances of those members exist within instances of their classes. They don't exist except within an instance, so to access them you need to access them via an instance of the class.
For example, something like this:
class Derived : public Base{
[some variables and functions]
friend void function(Derived& derived);
};
void function(Derived& derived)
{
int a = derived.get_a();
int b = derived.get_b();
// I don't know but even the following might work
int c = derived.a; // able to access field of friend's base class.
}
void test()
{
Derived* derived = new Derived();
function(*derived);
delete derived;
}
Your functions need to access a and b through an Instance of the class Derived as follows
void function()
{
Derived objectDerived;
int d[objectDerived.a][objectDerived.b];
[stuff]
}