简体   繁体   English

指向类的私有数据成员的指针

[英]Pointer to private data member of a class

Is it possible to declare a pointer to private data member of a class? 是否可以声明一个指向类的私有数据成员的指针? If so, how do you do it? 如果是这样,您该怎么做?

Yes, and the same way you would create any other pointer. 是的,与创建任何其他指针的方式相同。 The catch is, of course, that since the member is private, you can only create the pointer inside the class, where you can see the member. 美中不足的是,当然,由于成员是私有的,你只能创建类,在这里你可以看到部件的指针。

class A 
{
  public:
    int* getFooPtr()
    {
       return &foo;  // OK; Inside the class foo is visible
    }

  private:
    int foo;
};

int main()
{
   A a;

   int* p_foo1 = &a.foo; // Illegal; Outside the class, foo is private
   int* p_foo2 = a.getFooPtr(); // OK; getFooPtr() is a public member function
}

So it's possible to create pointers to private members, but only inside the class' member functions, and it is possible to return those created pointers from member functions. 因此,可以创建指向私有成员的指针,但只能在该类的成员函数内部,并且可以从成员函数返回那些创建的指针。 Whether or not it's a good idea to return pointers to private members is another question entirely (usually it's not a good idea). 将指针返回给私有成员是否是一个好主意,这完全是另一个问题(通常不是个好主意)。

You can access outside of the declaring class any non-static private member if you are exploiting the fact that C++ allows you to pass the address of the private member in explicit instantiation. 如果您利用C ++允许您在显式实例化中传递私有成员的地址的事实,则可以在声明类之外访问任何非静态私有成员。 However you won't be able to access static private members, since you have to use a pointer to member with this technique. 但是,您将无法访问静态私有成员,因为您必须使用此技术使用指向成员的指针。

struct A
{
     A() : x("proof!") {}
private:
     char const* x;
};

template class stow_private<A_x,&A::x>;

int main()
{
        A a;

        // Use the stowed private member pointer
        std::cout << a.*stowed<A_x>::value << std::endl;
};

You can find the implementation of stowed<> and details here: http://bloglitb.blogspot.hu/2010/07/access-to-private-members-thats-easy.html and https://gist.github.com/dabrahams/1528856 您可以在此处找到stowed<>的实现和详细信息: http : //bloglitb.blogspot.hu/2010/07/access-to-private-members-thats-easy.htmlhttps://gist.github.com / dabrahams / 1528856

Yes it's possible. 是的,有可能。 You'd have to return the pointer (or reference) from within the context of the class though, or someone who has friend access to the class. 但是,您必须从类的上下文中或者有friend访问该类的人中返回指针(或引用)。 That is because the variable itself is private and so can't be accessed otherwise. 这是因为变量本身是私有的,因此无法访问。

class C
{
public:
    int* getXPointer()
    {
        return &x;
    }

    int& getXReference()
    {
        return x;
    }

private:
    int x;
};


int main(int argc, char* argv[])
{
    C c;
    int* p = c.getXPointer();
    int& r = c.getXReference();
    assert(p == &r);
    return 0;
}

Yes, it is possible, as the previous answers have demonstrated. 是的,这是可能的,正如先前的答案所证明的那样。 But as Tyler McHenry asks, "Is it a good idea?". 但是正如泰勒·麦克亨利(Tyler McHenry)所问,“这是个好主意吗?”。 No, it isn't. 不,不是。 The member variables are declared private for a good reason, and subverting the encapsulation in this way will only lead to trouble. 有充分的理由将成员变量声明为私有,以这种方式破坏封装只会导致麻烦。

The wording of your question is rather confusing. 您问题的措辞颇为混乱。 When someone says "declare a pointer to something ", they are usually talking about type-related attributes of a pointer, as in "declare a pointer to int ". 当有人说“声明指向某物的指针”时,他们通常在谈论指针的类型相关属性,例如“声明为int的指针”。 The property of being private does not affect the type of a member at all, meaning that a member of type int is always just a member of type int , regardless of whether it is public or private. 私有的属性完全不影响成员的类型,这意味着int类型的成员始终只是int类型的成员,而不管它是public还是private。 This immediately means that a pointer of type int * can always be made to point to a public member of type int , or to a private member of type int . 这直接意味着类型的指针int *总是可以给点到类型的公共成员int ,或类型的私有成员int It doesn't matter at all whether the member is public or private. 成员是公开的还是私有的都没有关系。

Another ambiguity in the wording is that in C++ there are ordinary pointers (like int * ) and there are pointers of "pointer-to-member" type (like int MyClass::* ). 用语中的另一个歧义是C ++中有普通的指针(如int * )和“指针到成员”类型的指针(如int MyClass::* )。 When you say "a pointer to data member of a class", it is not clear what kind of pointer you are talking about: ordinary or pointer-to-member. 当您说“指向类的数据成员的指针”时,您不清楚所指的是哪种指针:普通指针或成员指针。 However, the above still applies to both kinds: both can easily point to public, protected or private members of the class. 但是,以上内容仍然适用于两种类型:两者都可以轻松地指向该类的公共成员,受保护成员或私有成员。 Privateness makes no difference. 私密性没有区别。

Again, the property of being "private" does not affect the type of the member, it only affects its accessibility when referred directly, by name. 同样,“私有”属性不会影响成员的类型,仅会直接影响按名称引用时的可访问性。 So, in order to make your pointer to point to a private data member of a class you have to initialize that pointer (or assign to it) in an area where that private data member is accessible: inside a method of the owning class or inside a friend function. 因此,为了使您的指针指向类的私有数据成员,您必须在可访问私有数据成员的区域中初始化该指针(或为其分配):在拥有类的方法中或内部朋友功能。

Yeah, if I understand you right, you'd like to be able to return a pointer to a private member of a class? 是的,如果我理解正确,您希望能够返回指向课程的私有成员的指针吗?

private:
        int hidden;
    public:
        int& unHide ()
        {
            return hidden;
        }

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

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