Questions tagged [friend-function]

In C++ friend function is a kind of function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public.

Wiki

A friend function in a C++ class is defined outside the scope of a class but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows

Example

// friend_functions.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class Point
{
    friend void ChangePrivate( Point & );
public:
    Point( void ) : m_i(0) {}
    void PrintPrivate( void ){cout << m_i << endl; }

private:
    int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
   Point sPoint;
   sPoint.PrintPrivate();
   ChangePrivate(sPoint);
   sPoint.PrintPrivate();
}

Tag usage

The tag can be used for programming related problems in implementation of friend functions, Theoretical questions such as "How to use friend functions" should be avoided

Read more

368 questions
0
votes
1 answer

frame grabbing with a callback function in c++. how does it communicate with a class?

I recently bought a GigE camera tha came bundles with its SDK. One of the examples explains how to grab frames from the camera through a callback function. It works in the following way: void OnImageGrabbed(Image* pImage, const void*…
nass
  • 1,453
  • 1
  • 23
  • 38
0
votes
2 answers

namespace, class member ADL conflict

#include namespace outside { struct A { int outer = 42; friend void print(A const& a, std::ostream& os) { os << "outside::A " << a.outer << '\n'; } }; namespace inside { struct A : outside::A { int inner…
dpj
  • 933
  • 1
  • 7
  • 14
0
votes
1 answer

friend member function in C++ - forward declaration not working

I'm having a situation similar to the one described in Specify a class member function as a friend of another class?. However in my case, class B needs to know class A since it's using it, so the solution given in that thread is not working for me.…
0
votes
1 answer

c++ friend function implemented in a class can't access privates

I'm trying to define 2 classes, declare a friend function in one of them and implement it in the other. I am actually trying to get exactly what found in this post working: How can friend function be declared for only one particular function and…
theexplorer
  • 359
  • 1
  • 5
  • 21
0
votes
0 answers

Friend Function accessing private variables

I wrote a small piece of code to test friend functions. It worked fine for methods that didn't belong to a specific class but when I tried to put it into a class all it can access is the public variables (just as any object would). #include…
ryan651
  • 121
  • 1
  • 1
  • 3
0
votes
2 answers

How to declare friend method when class definitions cross reference?

I want to define two classes, A and B. A has a data member which is a Class B object and is in-class initialised. A also has a method to retrieve the value in this B type data member and this method would be declared as a friend method in B. Here is…
God_of_Thunder
  • 753
  • 3
  • 20
  • 46
0
votes
2 answers

Friend function of templated structure with argument types depending on internals of the structure

I would like to define a templated structure with a friend function whose argument types are derived from types defined within the structure. The friend function should be callable without explicit type specification if the corresponding structure…
precarious
  • 598
  • 2
  • 14
0
votes
0 answers

Link error: undef reference - friend function

Just the salient details: Compilation: g++ -c memref_test.cpp g++ -c -I/home/chap/private/WDI/git -I/home/chap/private/WDI/git/include -I/usr/local/mysql/include -I/usr/local/include -I/home/chap/private/WDI/git…
Chap
  • 3,649
  • 2
  • 46
  • 84
0
votes
2 answers

no matching function call error

I'm trying to overload my << operator, but I want to use a helper function because I'm working with a tree, and that way my helper fn. can be recursive. But when I try to call the helper from the operator function I'm getting this no matching…
Scuba Steve
  • 1,541
  • 1
  • 19
  • 47
0
votes
1 answer

Trouble accessing private members with friend function in c++ operator<< overload

So I'm trying to overload the << operator. From all the sources I can see, the syntax is correct, but eclipse doesn't like it. I'm getting a couple errors: Polynomial::PrivateStruct* Polynomial::head is private And: struct Polynomial::PrivateStruct…
Scuba Steve
  • 1,541
  • 1
  • 19
  • 47
0
votes
1 answer

Access friend function

Need Help in accessing the function friend declared in Class_D from main. Guidance to proceed. /* Main.cpp */ #include #include "types.h" #include "Class_A.h" #include "Class_C.h" int main() { cout << "…
Prag Rao
  • 1,411
  • 3
  • 11
  • 10
0
votes
1 answer

Overloading comparison operators for stacks

I need some help here: I'm asked to do some overloading to comparison operators of 2 stacks. I have the syntax figured out, I'm just having trouble writing the definition. So please help me. At least to one operator overload and then I will do it…
Nicholas
  • 3,529
  • 2
  • 23
  • 31
0
votes
1 answer

accessing instance variable from another template class

(mostly pasted from accessing variable from another class template to separate two problems) i am trying to make a system of container classes that can be used with a data loader class to load data from text files here are the two classes of…
guskenny83
  • 1,321
  • 14
  • 27
0
votes
0 answers

accessing variable from another class template

i am trying to make a system of container classes that can be used with a data loader class to load data from text files here are the two classes of data: class Customer { //... }; class Tour { //... }; these are my two container…
guskenny83
  • 1,321
  • 14
  • 27
0
votes
1 answer

My friend function is not executing

I have declared a friend function in my header file, and defined it in my .cpp file, but when I compile I am told that the variables 'have not been declared in this scope'. It's my understanding that when a function is labeled a friend of a class,…
Kevin
  • 113
  • 1
  • 2
  • 6