473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

private member functions as friends

Can a make a private member function a friend? The code below suggests
not. I am using g++ 4.2.3

I realise that I can make the whole CFriend class a friend, or make my
friend function public but I don't really want to do either.

thanks

dan
#include <iostream>

class CClass;

class CFriend
{
private:
CClass& my_target;
void myWork(void);
public:
CFriend(CClass& target):my_target(target){;}
virtual ~CFriend(void){;}

void work(void){return myWork();}
};

class CClass
{
private:
void doWork(void)
{
std::cout << " CClass doWork() my_val = " << my_val++ << std::endl;
}

int my_val;

public:
CClass(int val):my_val(val){;}
virtual ~CClass(void){;}

friend void CFriend::myWork(void);
};

void
CFriend::myWork(void)
{
my_target.doWork();
}

int
main(int argc, char *argv[])
{
CClass first(1);

CFriend second(first);

second.work();
}
Jul 7 '08 #1
1 1421
On Jul 7, 12:05 pm, Dan Smithers <d...@foo.netwrote:
Can a make a private member function a friend? The code below suggests
not. I am using g++ 4.2.3

I realise that I can make the whole CFriend class a friend, or make my
friend function public but I don't really want to do either.

thanks

dan

#include <iostream>

class CClass;

class CFriend
{
private:
CClass& my_target;
void myWork(void);
public:
CFriend(CClass& target):my_target(target){;}
virtual ~CFriend(void){;}

void work(void){return myWork();}

};

class CClass
{
private:
void doWork(void)
{
std::cout << " CClass doWork() my_val = " << my_val++ << std::endl;
}

int my_val;

public:
CClass(int val):my_val(val){;}
virtual ~CClass(void){;}

friend void CFriend::myWork(void);

};

void
CFriend::myWork(void)
{
my_target.doWork();

}

int
main(int argc, char *argv[])
{
CClass first(1);

CFriend second(first);

second.work();

}
Member functions cannot be friends. Only classes or user-defined
functions can be used as friends.

Jul 7 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marc Schellens | last post by:
If I have a base class with a public virtual member function, a child class will grand access to its overridden memeber function even if its private in the child class, as the compliler cannot...
3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: zfareed | last post by:
Is it possible to print an array that is declared from a class type that also contains private data members? I have declared an array in the main function and then set the values for the array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.