简体   繁体   English

访问班级中的私人成员

[英]Accessing private members in a class

I need to access an object from a DLL, do some manipulations to the object and feed the object to another function. 我需要从DLL访问一个对象,对该对象进行一些操作并将该对象提供给另一个函数。 The trouble is the fields I need to change are private. 问题是我需要更改的字段是私有的。

I don't want to change the private modifier for the fields in the original class because the class was written a long time ago and is used in many places. 我不想为原始类中的字段更改private修饰符,因为该类是很久以前编写的并且在很多地方使用。 However, the place where I am manipulating the class I need most of the fields without protection (it is a hack). 但是,我操作类的地方我需要大部分没有保护的字段(这是一个黑客)。 What is the best way to do it? 最好的方法是什么?

Note: I am not allowed to change the original class 注意:我不允许更改原始课程

What is compiled in the DLL is not really important in this case, what matters is the header file you include. 在这种情况下,DLL中编译的内容并不重要,重要的是您包含的头文件 I suggest you change the header file of the class you're interested in so that the variables you need are public . 我建议你改变你感兴趣的类的头文件,这样你需要的变量就是public

Member access is checked by compiler and not by the linker, so all that matters is how the class is declared. 成员访问权限由编译器检查,而不是由链接器检查,因此重要的是如何声明类。 This does not require you to recompile the DLL or change the implementation of the class, but to just modify the header file (or a copy of the header file). 并不需要你重新编译DLL或改变类的实现,但只需修改头文件(或头文件的副本)。

一种方法:编写Getter / Setter函数

If you can see the original class you can make a mock class with the same bit pattern and cast the original object to the mock class object 如果您可以看到原始类,则可以使用相同的位模式创建一个mock类,并将原始对象强制转换为mock类对象

Example below 以下示例

Original class 原课

class ClassWithHiddenVariables
{
private:
    int a;
    double m;

}; 

Mock class 模拟课

 class ExposeAnotherClass
    {
    public:
        int a_exposed;
        double m_exposed;
    };

When you want to see members of the ClassWithHiddenVariables object, use reinterpret_cast to cast to ExposeAnotherClass 如果要查看ClassWithHiddenVariables对象的成员,请使用reinterpret_cast转换为ExposeAnotherClass

 ClassWithHiddenVariables obj;
    obj.SetVariables(10, 20.02);    
    ExposeAnotherClass *ptrExposedClass;
    ptrExposedClass = reinterpret_cast<ExposeAnotherClass*>(&obj);  
    cout<<ptrExposedClass->a_exposed<<"\n"<<ptrExposedClass->m_exposed;

The "Forger" hack from Exceptional C++ Style, Item 15 will work. 来自Exceptional C ++ Style的“Forger”攻击,第15项将起作用。 Although, it is illegal as it violates the One Definition Rule so I'd advise against doing this in production code. 虽然它违反了One Definition Rule是违法的,所以我建议不要在生产代码中这样做。

From page 106 来自第106页

// Example 15-1: Lies and Forgery
//
class X {
    // instead of including x.h, manually (and illegally) duplicates X's
    // definition, and adds a line such as:
    friend ::Hijack( X& );
};

void Hijack( X& x ) {
    x.private_ = 2;
}

Note how class X's private member "private_" is accessible in Hijack. 请注意,如何在Hijack中访问X类的私有成员“private_”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM