[英]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
. 在这个例子中,我希望
ResourceCache::Create
只能访问ResourceDesc::Create
,但不能访问所有ResourceDesc
。
Is that possible? 那可能吗?
C++ doesn't give you access control this fine-grained. C ++没有为您提供细粒度的访问控制。 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. C ++不允许这样做。
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. 我的直接想法是,如果你想这样做,它至少可能表明你的
ResourceDesc
(以及可能还有ResourceCache
)可能在很大的一面,可能会做多个类真的应该。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.