Solved

Friends- How to allow a private function of one class to be called from another class

Posted on 1997-02-25
1
236 Views
Last Modified: 2010-04-10
      I have a piece of machine control code running under Win95 (will someday run under WinNT when it supports the DAQ hardware) written with VC++ 4.x. The main initialization/splash dialog routine does all the rudimentary hardware setup, registry reads, database table reads, etc. But, there is a debug option that can be enabled from an environment var that will skip the machine hardware setup and will bypass all future calls to the hardware to allow the software to be run on a desktop w/o the machine.
         The problem comes in b/c there is a button I have given the user to allow them to toggle the system out of Debug Mode and into Run Mode. Well, if the system started in debug mode, the hardware was never initialized so there will be bunches of errors. Right now I have the hardware init code duplicated in two places (hard to maintain). How can I use the C++ Friend operator to allow the button click procedure in another class to access the hardware init procedure in the startup routine? Should I use another method? The online docs are vague in the friend operator usage. I tinkered with it, but was never successful.Thanks. Code is available upon request...
0
Comment
Question by:maknight
1 Comment
 

Accepted Solution

by:
meeroh earned 50 total points
ID: 1162120
Friend declaration:

class foo {
      friend int bar (int i);
};

this declaration will declare the function
int bar (int i);
in such a way that it has access to all the private members of class foo. It will -not- give the bar function a this pointer (unless bar is itself a member function, see below). Inside the body of bar you can freely access private members of any objects of class foo you have.

A special case, when bar is itself a member function... for example:

class baz {
int bar (int i);
};

class foo {
friend int baz::bar (int i);
};

This gives access to private members of foo inside the baz::bar function. Frequently, you will want all functions of one class to access the private members of another class, in which case you can use the following syntax:

class foo {
friend class bar;
};
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question