?
Solved

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

Posted on 1997-02-25
1
Medium Priority
?
248 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 100 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.
Suggested Courses

649 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