对象可以更改其他对象的私有数据吗?

时间:2015-12-13 12:37:52

标签: c++

如果C类可以容纳另一个C对象,该子级是否可以更改其父级的私有数据成员?

下面的代码有效,但我不确定我所做的事情是否有危险。 (即未定义的行为)

class C {
public:
    C(C *parent, int num) : _parent(parent), _i(num), _child(nullptr) {}
    ~C() {
        if(_child) {
            delete _child;
            _child = nullptr;
        }
    }

    int get() const { return _i; }

    void addChild() {
        if(!_child) _child = new C(this, 0);
    }

    C* getChild() {
        return _child;
    }

    void setParent(int num) {
        _parent->_i = num; //accessing parent private data
    }

private:
    int _i;
    C *_parent;
    C *_child;
};

int main()
{
    C c(nullptr, 1);
    c.addChild();
    c.getChild()->setParent(25);

    std::cout << c.get() << "\n"; //output is 25 as expected

    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:4)

是的,它可以。 12-13 18:25:09.352: D/com.amazon.identity.auth.device.appid.APIKeyDecoder.PII(3337): APIKey:<obscured> 12-13 18:25:09.352: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): verifySignature for packageName=com.example.helloworldsample 12-13 18:25:09.352: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): verifySignature Sha256 for packageName=com.example.helloworldsample 12-13 18:25:09.352: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): verifyPayload for packageName=com.example.helloworldsample 12-13 18:25:09.352: D/com.amazon.identity.auth.device.appid.APIKeyDecoder.PII(3337): Signature checking.:<obscured> 12-13 18:25:09.362: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): num sigs = 1 12-13 18:25:09.362: D/com.amazon.identity.auth.device.appid.APIKeyDecoder.PII(3337): Fingerprint checking:<obscured> 12-13 18:25:09.362: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): num sigs = 1 12-13 18:25:09.362: D/com.amazon.identity.auth.device.appid.APIKeyDecoder.PII(3337): Fingerpirints checking:<obscured> 12-13 18:25:09.362: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): scopes has no mapping in json, returning null array 12-13 18:25:09.362: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): perm has no mapping in json, returning null array 12-13 18:25:09.362: I/com.amazon.identity.auth.device.authorization.ThirdPartyServiceHelper(3337): Attempting to retrieve remote Android service. Ignore cached service=true 12-13 18:21:43.992: D/com.amazon.identity.auth.device.authorization.ThirdPartyServiceHelper(3337): getAuthorizationServiceInstance 12-13 18:21:43.992: I/com.amazon.identity.auth.device.authorization.ThirdPartyServiceHelper(3337): Number of services found : 0 12-13 18:21:43.992: I/com.amazon.identity.auth.device.authorization.ThirdPartyServiceHelper(3337): Number of MAP services to compare = 0 12-13 18:21:43.992: I/com.amazon.identity.auth.device.authorization.ThirdPartyServiceHelper(3337): Returning no service to use 12-13 18:21:44.012: D/com.amazon.identity.auth.device.authorization.ThirdPartyAuthorizationHelper.PII(3337): Created UUID for request:<obscured> 12-13 18:21:44.012: D/com.amazon.identity.auth.device.authorization.AuthorizationHelper.PII(3337): Generating Redirect URI:<obscured> 12-13 18:21:44.022: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): num sigs = 1 12-13 18:21:44.022: D/com.amazon.identity.auth.device.appid.APIKeyDecoder.PII(3337): Fingerprint checking:<obscured> 12-13 18:21:44.022: I/com.amazon.identity.auth.device.appid.APIKeyDecoder(3337): num sigs = 1 12-13 18:21:44.022: D/com.amazon.identity.auth.device.appid.APIKeyDecoder.PII(3337): Fingerprint checking:<obscured> 12-13 18:21:44.032: D/com.amazon.identity.auth.device.authorization.AuthorizationHelper.PII(3337): Generating OAUTH2 URL:<obscured> 12-13 18:21:44.032: I/com.amazon.identity.auth.device.authorization.ThirdPartyAuthorizationHelper(3337): Starting External Browser 12-13 18:21:46.694: W/com.amazon.identity.auth.device.authorization.AuthorizationActivity(3337): onCreate 12-13 18:21:46.694: D/com.amazon.identity.auth.device.authorization.AuthorizationActivity.PII(3337): Received response from WebBroswer for OAuth2 flow:<obscured> 12-13 18:21:46.694: D/com.amazon.identity.auth.device.authorization.AuthorizationResponseParser.PII(3337): Received response from WebBroswer for OAuth2 flow:<obscured> 12-13 18:21:46.694: D/com.amazon.identity.auth.device.authorization.AuthorizationResponseParser.PII(3337): Code extracted from response:<obscured> 12-13 18:21:46.694: E/AuthorizationListener(3337): inside error method 个成员可以访问同一个类的实例。实例可以访问另一个实例数据。

例如,这就是移动构造函数/赋值运算符的工作方式:窃取数据并将其从原始对象中删除,以避免对同一数据的双重引用。

相关问题