This question already has answers here:
Specify a class member function as a friend of another class?
(8 answers)
Closed 3 years ago.
I'm taking a C++ course, and I came across something I can't wrap my head around. I've tried searching for an answer, but I've come up short.
class A {
friend void C::dec(A&);
private:
int field;
};
class C {
public:
void dec(A& a);
};
void C::dec(A& a) { a.field--; } <-- member A::field is inaccessible
I am aware that for this to work, class A should be declared before, but defined after, class C. But I'm struggling to understand why.
So why is the class member A::field inaccessible when class A is defined before class C?
The problem is not the friend declaration, it's just C is unknown where you declare it in A.
So you define C and forward declare A, then just define C as you already did.
class A;
class C {
public:
void dec(A& a);
};
class A {
friend void C::dec(A&);
private:
int field;
};
void C::dec(A& a) { a.field--; }
Related
Is it possible to create a class that has a friend function of a forward declared class? If I wanted to create two classes that have friend functions of each other, how would I do that? This attempt fails with the message invalid use of incomplete type 'class Y':
class Y;
class X
{
public:
void a();
friend void Y::b();
};
class Y
{
public:
void b();
friend void X::a();
};
I also tried forward declaring void Y::b(); right after class Y; which similarly did not work. Would this example be an indicator of bad design on my part or just a limitation of C++?
This question already has answers here:
Declaring a variable with "class " keyword vs Declaring one without "class" keyword in function signatures
(4 answers)
Closed 8 years ago.
I'm studying code given to me and I see:
AFPSGameMode::AFPSGameMode(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP) { }
I'm specially curious about the use of the class keyword. Is this standard C++ and if so what does it mean?
Thank you.
The class keyword is allowed here, it's just rare to see it placed here since it can either be completely omitted (if this class has been previously declared) or replaced with the forward declaration:
void foo(const class FPostConstructInitializeProperties& p){ ... }
which is equivalent to:
class FPostConstructInitializeProperties; // <-- forward declaration
void foo(const FPostConstructInitializeProperties& p){ ... }
Don't get confused with the weird naming conventions. The snippet you have provided expresses something like this:
class B{
public:
B(){ }
B(const B& b){ };
};
class A{
public:
B my_b;
A(const class B& b) : my_b(b) { } // <-- class keyword in ctor's param decl.
};
that could be used for example like this (but I guess it's clear enough already):
int main() {
B b;
A a(b);
}
As it was old C, if you have struct say,
struct account
{
int field;
..
};
You can use it for creating its variables (objects) like,
account obj;
or,
struct account obj;.
Same is for class, you may use it, or avoid it. But it is usually not used, but permitted.
This question already has answers here:
Why does C++ not allow inherited friendship?
(9 answers)
Closed 8 years ago.
There are three classes:
class A
{
friend I_B;
protected:
void* mData;
};
class I_B
{
void foo() = 0;
};
class B_Impl : public I_B
{
B_Impl( A* value )
:
mData( value->mData ) <--- ERROR
{
}
void foo() { mData->DoSomething() };
protected:
void* mData;
};
At compile time I get an error in the constructor, that mData is a protected member.
Please explain me please why it happens.
Can I get access to protected members using "friendship" of the base class?
Friendship is not inherited. If you want B_Impl to be a friend of A you must declare B_Impl as a friend.
Friendship is also not transitive: your friend's friend is not necessarily your friend.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is this key-oriented access-protection pattern a known idiom?
I have class A and class B. I want class A to access one of class B's private functions; but only that, not everything else. Is that possible?
Some kind of example:
class A {
//stuff
};
class B {
int r; // A cant use this
MagicFriendKeyword A void func(); // A can use this
public:
...
};
If there is one (or few) members functions in class A, that want to use class B's private member functions, then you can declare those one/few functions as friend. E.g.
class B {
// ...
friend void A::mutateB( B * );
// ...
};
See http://en.wikipedia.org/wiki/Friend_function
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Friend scope in C++
Are friends in C++ mutual?
class bar
{
private:
void barMe();
};
class foo
{
private:
void fooMe();
friend bar;
};
In the above example foo class can't call barMe()
You need to define the classes this way in order that the friend be mutual:
class foo; // forward
class bar
{
private:
void barMe();
friend foo;
};
class foo
{
private:
void fooMe();
friend bar;
};
The friend relationship is only one-way in general - but there is nothing to stop you declaring Class A a friend of class B AND class B a friend of class A. So a mutual relationship can be established