Access protected data member via friend member function - c++

EDIT:
The original program has multiple files as shown in here
I'm trying to access protected data member in my class from a friend member function of a different class.
I keep getting an access error:
9:32: error: invalid use of incomplete type 'class b' 5:7: error:
forward declaration of 'class b' In member function 'void b::f(a*)':
12:13: error: 'int a::i' is protected 20:47: error: within this
context
This is my code:
// Example program
#include <iostream>
#include <string>
class b;
class a{
public:
friend void b::f(a* pointer);
protected:
int i = 6;
};
class b{
public:
void f(a* pointer){std::cout<<pointer->a::i<<std::endl;}
};
int main()
{
a a1;
b b1;
b1.f(&a1);
}

The issue is only superficially related to friend. It's rather due to circular dependencies of class definitions and member functions; a needs to know the definition of b because it refers to b::f. But the definition of b::f needs the definition of a, as it refers to a::i. Luckily, definitions of a class and its member functions can be separated from each other.
You can take the following measures to tackle this step by step:
declare class a;
define class b { /* ... */ };, but without defining its member functions (as they need access to a's definition
define class a { /* ... */ };, possibly with inline member function definitions
and finally define b's member functions.
This is how it looks like:
// Step 1:
class a;
// Step 2:
class b{
public:
void f(a* pointer);
};
// Step 3:
class a{
public:
friend void b::f(a* pointer);
protected:
int i = 6;
};
// Step 4:
void b::f(a* pointer)
{
std::cout<<pointer->a::i<<std::endl;
}

At the point where you try to declare the method of b a friend via friend void b::f(a* pointer);, the class b is incomplete. You cannot refer to members yet. On the other hand, b only needs a forward declaration for a:
// Example program
#include <iostream>
#include <string>
class a;
class b{
public:
void f(a* pointer);
};
class a{
public:
friend void b::f(a* pointer);
protected:
int i = 6;
};
void b::f(a* pointer){std::cout<<pointer->a::i<<std::endl;}
int main()
{
a a1;
b b1;
b1.f(&a1);
}

Try this. You are trying to reference b::f before class b is defined.
// Example program
#include <iostream>
#include <string>
class a;
class b{
public:
void f(a* pointer);
};
class a{
public:
friend void b::f(a* pointer);
protected:
int i = 6;
};
void b::f(a* pointer)
{std::cout<<pointer->a::i<<std::endl;}
int main()
{
a a1;
b b1;
b1.f(&a1);
}

Related

Why Friend Function cannot access private members of a class

class A{
public:
void printer(){
B obj;
obj.private_data = 10; // <- fails, says member inaccessible
}
}
class B{
friend void A::printer();
private:
int private_data;
}
is it possible for printer function to access private members of class B? i tried to pass an obj of B as arg to printer but it still failed
Class A doesn't know about B to use it. Hence, postpone the definition of the function printer() until you define B, and if you need an instance of B to be a member var in A then make a forward declaration for B to declare a B* in A.
Hence, use something like what follows:
class A {
public:
void printer();
};
class B {
friend void A::printer();
private:
int private_data;
};
void A::printer() {
B obj;
obj.private_data = 10; // <- No longer fails
std::cout << obj.private_data;
}
int main() {
A a;
a.printer();
}
Demo
Why Friend Function cannot access private members of a class?
They can, but you may need to split the definition of the class up a bit.
Imaginary files added:
Define A (file a.hpp):
class A {
public:
void printer();
};
Define B (file b.hpp):
#include "a.hpp" // B must see the definition of A to befriend a member function
class B {
friend void A::printer();
private:
int private_data;
};
Define A's member function (file a.cpp):
void A::printer() {
B obj;
obj.private_data = 10;
}
To access B, you first need to define it. Thus, you can just declare the method printer and define it after you have defined the class B.
class A {
public:
void printer();
};
class B {
private:
friend class A;
int private_data;
};
void A::printer() {
B obj;
obj.private_data = 10;
}
Note, you probably want to move your methods out of your class definition anyways and into a separate .cpp file. Methods defined inside the class are implicitly marked as inline which might not be what you expect.

How to access private class variable from non-member function [duplicate]

This question already has answers here:
When should you use 'friend' in C++?
(31 answers)
Closed 1 year ago.
From the below code snipped, need to access member function of class A::B. Since class B is private unable to create object for it from non-member function. Kindly help me in getting resolve out of this without moving Class B into public
Code below for reference
A.h
#include <iostream>
class A
{
public:
A();
void Func();
private:
class B;
B *b;
};
A.cpp
#include "A.h"
#include "AB.h"
A::A()
{
b = new B(this);
}
void A::Func()
{
b->Func();
}
AB.h
#include "A.h"
class A::B
{
public:
B(A* a);
void Func();
private:
A* ptr;
};
AB.cpp
#include "AB.h"
A::B::B(A* a):ptr(a)
{
}
void A::B::Func()
{
std::cout<<"Do nothing ";
}
static void call_sample()
{
A::B* tmp;
//access member function of class A::B
}
Error below for reference:
In file included from AB.cpp:2:0:
AB.h: In function ‘void call_sample()’:
AB.h:3:10: error: ‘class A::B’ is private
class A::B
^
AB.cpp:15:4: error: within this context
A::B* tmp;
B is a nested class from A, and it is private, so, as a definition, it is not accessible from the outside. If you need to use B you can decleare B in the public part of A, and then you can use A::B* type.

Cyclic dependency involving global objects C++

So, I'm in this situation right now. I have two classes A and B. The B is subclass of A and there's also a global object of the B class which is initialized in the main. A function of the A class uses that global object and calls its functions. In what order do I have to write the declarations in order for the compiler to read everything?
I keep getting the same errors whatever I try. Namely:
- (x) does not name a type
- invalid use of incomplete type (x)
- forward declaration of (x)
Code example:
class B;
B* B_GLOBAL;
class A{
public:
void A_function(){
B_GLOBAL->B_function();
}
private:
};
class B : public A{
public:
void B_function();
private:
};
int main(void){
B_GLOBAL = new B;
return 0;
}
Move the definition of A_function below the declaration of B:
class B;
B* B_GLOBAL;
class A{
public:
void A_function();
private:
};
class B : public A{
public:
void B_function();
private:
};
void A::A_function(){
B_GLOBAL->B_function();
}
int main(void){
B_GLOBAL = new B;
return 0;
}

Protected member function address in derived class is not accessible

#include <iostream>
class A {
protected:
void foo()
{}
};
class B : public A {
public:
void bar()
{
std::cout << (&A::foo) << std::endl;
}
};
int main()
{
B b;
b.bar();
}
Here I am trying to get address of protected member function of base class. I am getting this error.
main.cpp: In member function ‘void B::bar()’:
main.cpp:5: error: ‘void A::foo()’ is protected
main.cpp:13: error: within this context
make: *** [all] Error 1
Changing foo to public works. Also printing &B::foo works. Can you please explain why we can't get address of protected member function of base class?
B is allowed to access protected members of A as long as the access is performed through an object of type B. In your example you're trying to access foo through A, and in that context it is irrelevant whether B derives from A or not.
From N3337, §11.4/1 [class.protected]
An additional access check beyond those described earlier in Clause 11 is applied when a non-static data member or non-static member function is a protected member of its naming class (11.2) As described
earlier, access to a protected member is granted because the reference occurs in a friend or member of some class C. If the access is to form a pointer to member (5.3.1), the nested-name-specifier shall denote C or a
class derived from C. All other accesses involve a (possibly implicit) object expression (5.2.5). In this case, the class of the object expression shall be C or a class derived from C. [Example:
class B {
protected:
int i;
static int j;
};
class D1 : public B {
};
class D2 : public B {
friend void fr(B*,D1*,D2*);
void mem(B*,D1*);
};
// ...
void D2::mem(B* pb, D1* p1) {
// ...
int B::* pmi_B = &B::i; // ill-formed
int B::* pmi_B2 = &D2::i; // OK
// ...
}
// ...
—end example]
Your example is very similar to the code in D2::mem, which shows that trying to form a pointer to a protected member through B instead of D2 is ill-formed.
I was curious and tried the following example:
#include <iostream>
using namespace std;
class A {
public:
void foo()
{
}
};
class B : public A {
public:
void bar()
{
printf("%p\n", (&A::foo));
printf("%p\n", (&B::foo));
}
};
int main()
{
B b;
b.bar();
}
Actually, I see that &A::foo == &B::foo, so for protected member of base class you can use derived class member to take address. I suppose in case of virtual functions this will not work
Seems I found the answer. If we could get pointer of member function we can call it for other objects of type A (not this) which is not allowed.
It is not allowed to call protected member function in derived classes for objects other than this. Getting pointer would violent that.
We can do something like this:
#include <iostream>
class A {
protected:
void foo()
{}
};
class B : public A {
public:
void bar()
{
void (A::*fptr)() = &A::foo;
A obj;
(obj.*fptr)();
// obj.foo(); //this is not compiled too.
}
};
int main()
{
B b;
b.bar();
}

incomplete type error , when using friend functions

#include <stdio.h>
class B;
class A;
class A
{
int a;
friend int B::f();
};
class B
{
int b;
class A x;
public:
int f();
};
int B::f()
{
// ...
}
main()
{
class B b;
b.f();
}
ERRORS:
a.cpp:9: error: invalid use of incomplete type ‘struct B’
a.cpp:2: error: forward declaration of ‘struct B’
The issue cannot be solved by placing definition of B before A as
B has an object of type A.
For this example making B a friend class would do, but in
my real code I have more member functions in B (so I need alternative solution).
Finally, can somebody give me links that explain what the compiler does when it comes
across a forward declaration, declaration, definition.
You simply cannot do what you want to do as-is. To make that friend function declaration in class A the nature of class B needs to be known prior to the definition of class A. To make class B contain an instance of class A the nature of class A must be known prior to the definition of class B. Catch-22.
The former does not apply if you make class B a friend class of class A. The latter does not apply if you modify B to contain a pointer or reference to an instance of class A.
DefineB before A, and declare a pointer to A as member data of B:
class A; //forward declaration
class B
{
int b;
A *px; //one change here - make it pointer to A
public:
int f();
};
class A
{
int a;
friend int B::f();
};
Or, you could make the entire class B a friend of A, that way you don't have to make the member data pointer to A.
class B; //forward declaration
class A
{
int a;
friend class B;
};
class B
{
int b;
A x; //No change here
public:
int f();
};
Forward declare class A; define class B; define class A; define B::f.
#include <cstdio>
class A;
class B
{
int b;
public:
int f();
};
class A
{
int a;
friend int B::f();
};
int B::f()
{
}
main()
{
class B b;
b.f();
}