简体   繁体   English

C ++:允许访问类的受保护成员而不是私有成员

[英]C++: Allowing Access to Protected Members of Class and not Private Members

I know you can do this with inheritance, but you're meant to use inheritance for except for 'is a' circumstances. 我知道你可以通过继承来做到这一点,但你的意思是使用继承,除了'是'的情况。 I also know there are friends, but they allow access to private members as well. 我也知道有朋友,但他们也允许访问私人会员。

Is there any way of doing this (Allowing Access to Protected Members of Class and not Private Members)? 有没有办法做到这一点(允许访问受保护的类成员而非私人成员)?

To reword the question, I have class 1 and class 2. I want class two to have access to class 1's protected and public members, but not it's private members. 为了改写这个问题,我有1级和2级。我希望第二级能够访问第1类的受保护和公共成员,但不是它的私有成员。 How would I do this? 我该怎么做?

It's not elegant, but this might work for you: 它不优雅,但这可能对你有用:

class B;

class A {
protected:
    int x;
private:
    int y;
};

class A_wrapper : public A {
    friend B;
};


class B {
public:
    A_wrapper a;
    int foo() {
        a.x;   // Ok
        a.y;   // Compiler error!
    }
};

Oli's provided a closer solution (+1), but you could also approach it using a selective friend: Oli提供了一个更接近的解决方案(+1),但您也可以使用选择性朋友来接近它:

#include <iostream>

class t_thing;

class t_elsewhere {
public:
    void print(const t_thing& thing);
};

class t_thing {
public:
    class t_selective_friend {
        static int Prot(const t_thing& thing) {
            return thing.prot;
        }

        friend class t_elsewhere;
    };
public:
    int publ;
protected:
    int prot;
protected:
    int priv;
};

void t_elsewhere::print(const t_thing& thing) {
    std::cout << t_thing::t_selective_friend::Prot(thing) << std::endl;
}

int main() {
    t_thing thing;

    thing.publ; /* << ok */
    thing.prot; /* << error */
    thing.priv; /* << error */
    t_elsewhere().print(thing); /* << ok */
    return 0;
}

Sometimes that verbosity/control is good… 有时候冗长/控制很好......

Long ago, on this very website, I presented a scheme using a Key . 很久以前,在这个网站上,我使用Key提出了一个方案。 The idea is that the main class documents which parts of the interface are accessible publicly and which necessitates a key, and then friendship to the key is granted to those who need it. 这个想法是,主类文档可以公开访问界面的哪些部分,并且需要密钥,然后对需要它的人授予对密钥的友好。

class Key { friend class Stranger; Key() {} ~Key() {} };

class Item {
public:
    void everyone();

    void restricted(Key);
private:
};

Now, only Stranger may use the restricted method, as is demonstrated here: 现在,只有Stranger可以使用受restricted方法,如下所示:

class Stranger {
public:
    void test(Item& i) {
        Key k;
        i.restricted(k);
    }

    Key key() { return Key(); }

    Key _key;
};

class Other {
    void test(Item& i) {
        Stranger s;
        i.restricted(s.key()); // error: ‘Key::~Key()’ is private
                               // error: within this context
    }

    void test2(Item& i) {
        Stranger s;
        i.restricted(s._key); // error: ‘Key::~Key()’ is private
                              // error: within this context
                              // error:   initializing argument 1 of ‘void Item::restricted(Key)’
    }
};

It is a very simple scheme which allows a much finer grained approach that full friendship. 这是一个非常简单的方案,允许更精细的方法,充分的友谊。

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

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