?
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
?
247 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

765 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