Access to private members and methods of a C++ struct or class
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

privablic logo

Access to private members and methods in C++

How to use it

Copy privablic.h into your project and #include "privablic.h".

Suppose you know the implementation of a class (or a struct) like that:

class Sheep {
    Sheep(string name) { this->name = name; };
    string name;
    void baa() { cout << name << ": Baa! Baa!" << endl; };
    static int TOTAL;
    static void FlockCount() {
      cout << "sheperd actually counted " <<
      TOTAL << " sheeps" << endl;

int Sheep::TOTAL = 42;

you only have to map some stubs according to types of members and/or methods signatures:

Instance Member

struct Sheep_name { typedef string (Sheep::*type); };
template class private_member<Sheep_name, &Sheep::name>;

Instance Method

struct Sheep_baa { typedef void(Sheep::*type)(); };
template class private_method<Sheep_baa, &Sheep::baa>;

Static Instance Member

struct Sheep_TOTAL { typedef int *type; };
template class private_member<Sheep_TOTAL, &Sheep::TOTAL>;

Static Instance Method

struct Sheep_FlockCount { typedef void(*type)(); };
template class private_method<Sheep_FlockCount, &Sheep::FlockCount>;

just obtain a Sheep:

Sheep dolly = Sheep("Dolly");

// now we have a sheep under our complete control:

// - change dolly's identity
dolly.*member<Sheep_name>::value = "Lilly";

// - make dolly baa

// - steal dolly
int flockCount = *member<Sheep_TOTAL>::value -= 1;

// - let the sheperd realize it


Lilly: Baa! Baa!
sheperd actually counted 41 sheeps

Tested compilers which works with


Works under OSX


Unkwown (please try and pull request to let me know :)


Unkwown (please try and pull request to let me know :)


Johannes Schaub (litb)

Dave Abrahams

Christof Warlich


Copyright 2017 Michelangelo Altamore. It may be redistributed under the terms specified in the LICENSE file.