Solved

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

Posted on 1997-02-25
1
222 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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now