C++ - Access private members of class from outside the class

12,208

Solution 1

If those members are made private and you need access to them, you're doing something wrong, so my suggestion is that you don't.

Now let's get serious - there are some ways - DISCLAIMER: not all are safe or portable or guaranteed to work:

1) Preprocessor abuse

#define class struct
#define private public
#include "CameraAcquisition.h"
#undef class
#undef private

2) Fake class

class MyCameraAcquisition {
public: //<--- public in your class
    /* MEMBERS */
    CvSize size;
    CvCapture *device;
    CvScalar hsv_min,hsv_min2,hsv_max,hsv_max2;

    /* METHODS */

public:
    CameraAcquisition();
    ~CameraAcquisition();
    int findBall();
};

CameraAcquisition prvt;
MyCameraAcquisition publ = (MyCameraAcquisition&) prvt;

3) Template abuse posted by litb - http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html

This one is, actually, and surprisingly, standard-compliant.

Solution 2

You should not want to access private mebers of objects. Consider providing public getter/setter member functions that outside code can use to affect the private member variables

Alternatively, you can make your support function a member of the class, so it has automatic access to the private members.

If you really want to access private members, you can use the friend keyword to declare another class or function as friend of the current class

class A
{
  int a; // private
  friend class B;
  friend void f(A*);
};

Here both class B (i.e. its member functions) and free function f can access A::a

Share:
12,208
paxilpaz
Author by

paxilpaz

Updated on June 04, 2022

Comments

  • paxilpaz
    paxilpaz about 2 years

    I'd like to know if there's any way to access a private member of a class from outside the class. I'll explain my problem.

    I have a .hpp file which contains the definition of the class along with its private members and public function (which are the only one I'd like to export). In the corrisponding .cpp I have to use some "support" function which need access to the private members of the class defined in the .hpp.

    Here's part of my code:

    --- .hpp ---

    namespace vision {
    class CameraAcquisition {
    
        /* MEMBERS */
        CvSize size;
        CvCapture *device;
        CvScalar hsv_min,hsv_min2,hsv_max,hsv_max2;
    
        /* METHODS */
    
    public:
        CameraAcquisition();
        ~CameraAcquisition();
        int findBall();
    };
    }
    

    --- .cpp ---

    #include "CameraAcquisition.hpp"
    
    using namespace vision;
    
    IplImage *grabFrame() {
         // code here
    }
    
    IplImage *convertToHSV(IplImage *origin) {
    // code here
    }
    
    IplImage *calculateThresholdedImage(IplImage *converted) {
    // code here
    }
    

    What I need is for these three functions to access the members of the class CameraAcquisition. Is there any way to do it? Any suggestions will be appreciated. Thank you all

    EDIT Sorry, I forgot an important piece of information here. In the source file, findBall() must call those methods. I defined those methods to make the code easier to read. I cannot declare those methods in the class definition because I don't want to export them. If I declare them in a "private" block everything works fine, but maybe it's not correct to that (I don't see the point in providing an header file with private methods.