Code Monkey home page Code Monkey logo

privablic_cpp's Introduction

privablic logo

Access to private members and methods in C++

How to use it

Copy privablic.h into your project and #include "privablic.h", and for easy reading, add:

using namespace privablic;

Now, suppose you know the implementation of a class (or a struct) like that:

class Sheep 
{
public:
    Sheep(std::string name_) : name{ std::move(name_) } {}
    
private:
    // Data
    std::string name;
    static int TOTAL;

    // Functions
    void baa() { std::cout << name << ": Baa! Baa!\n"; };
    
    static void FlockCount() 
    {
      std::cout << "sheperd actually counted " << TOTAL << " sheep\n";
    }
};

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>;

Then, using an instance of Sheep, you can access the private members like this:

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
(&dolly->*func<Sheep_baa>::ptr)();

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

// - let the sheperd realize it
(*func<Sheep_FlockCount>::ptr)();

Output:

Lilly: Baa! Baa!
sheperd actually counted 41 sheeps

Tested compilers which works with

clang

Works under OSX

gcc

GCC 7.x (at least!)

msvc

Visual Studio 2019 (at least!)

Credits

License

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

privablic_cpp's People

Contributors

altamic avatar matthew-limbinar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.