简体   繁体   中英

Can I make a member function only accessible through another class member function?

As far as my knowledge goes, a friend function will give the function free access to the entire class. Can I scope this friend function by only letting it access a single member function?

For example:

class ResourceCache {
    Resource* Create(ResourceDesc) {
        ...
        return ResourceDesc.Create();
    }
}

class ResourceDesc {
    ...
    Resource* Create();
}

In this example, I want ResourceCache::Create to only have access to ResourceDesc::Create , but not to all of ResourceDesc .

Is that possible?

C++ doesn't give you access control this fine-grained. Don't worry too much about getting the access control precisely right, it's all code you or your collaborators have written anyway, what bad thing are you afraid might happen?

C++ doesn't allow that.

My immediate thought would be that if you want to do this, it's at least a possible indication that your ResourceDesc (and possibly ResourceCache as well) may be on the large side and may do more than one class really should.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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