简体   繁体   English

我是否只能通过其他类成员函数访问成员函数?

[英]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.

相关问题 为什么我不能将此成员函数作为另一个类的朋友? - Why can't I make this member function friend of another class? 如何在另一个类的静态成员函数中传递非静态成员函数的函数指针 - How can I pass function pointer of a non static member function in a static member function in another class 如何将一个 class 的成员 function 作为参数传递给另一个 ZA2F2ED4F8EBC2CBBD4ZC2 的成员 function? - How can I pass member function of one class as an argument to member function of another class? How can I call a non-static member function of a class from a static member function of another class? - How can I call a non-static member function of a class from a static member function of another class? 如何通过绑定另一个成员函数的参数来制作C ++成员函数? - How can I make a C++ member function by binding the arguments of another member function? 无法访问派生类中受保护的成员函数地址 - Protected member function address in derived class is not accessible 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 我可以检测成员函数是否存在,并且朋友可以用C ++访问吗? - Can I detect if a member function exists and is accessible to a friend in C++? 使用友元函数迭代另一个类的私有成员 - iterating through private member of another class using friend function 为什么PRIVATE成员函数不能成为另一个类的朋友函数? - Why can't a PRIVATE member function be a friend function of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM