Solved

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

Posted on 1997-02-25
1
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

690 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