Matthieu M.在我之前看过的这个答案中提出了一种访问保护模式,但从未有意识地考虑过一种模式:
class SomeKey {
friend class Foo;
SomeKey() {}
// possibly make it non-copyable too
};
class Bar {
public:
void protectedMethod(SomeKey);
};
Run Code Online (Sandbox Code Playgroud)
这里只有一个friend关键类可以访问protectedMethod():
class Foo {
void do_stuff(Bar& b) {
b.protectedMethod(SomeKey()); // fine, Foo is friend of SomeKey
}
};
class Baz {
void do_stuff(Bar& b) {
b.protectedMethod(SomeKey()); // error, SomeKey::SomeKey() is private
}
};
Run Code Online (Sandbox Code Playgroud)
它允许更多的细粒度访问控制不是制造Foo一个friend的Bar,避免了更复杂的代理模式.
有谁知道这种方法是否已经有一个名称,即,是一个已知的模式?
我们是否可以增加这种面向密钥的访问保护模式的可重用性:
class SomeKey {
friend class Foo;
// more friends... ?
SomeKey() {}
// possibly non-copyable too
};
class Bar {
public:
void protectedMethod(SomeKey); // only friends of SomeKey have access
};
Run Code Online (Sandbox Code Playgroud)
为了避免持续的误解,这种模式不同于律师 - 客户的习惯用语:
(在这个问题中提出了一个侧面讨论,因此我打开了这个问题.)
默认情况下,类的成员在c ++中是私有的.
因此,我想知道是否有任何可能的用法来创建一个将其所有成员(变量和函数)默认设置为private的类.
换句话说,没有任何关键字是否存在任何有意义的类定义public,protected或者private?
显然,这种面向密钥的访问保护模式:
class SomeKey {
friend class Foo;
SomeKey() {}
// possibly non-copyable too
};
class Bar {
public:
void protectedMethod(SomeKey); // only friends of SomeKey have access
};
Run Code Online (Sandbox Code Playgroud)
......还没有一个已知的名字,因此我想找到一个好的名字,所以我们可以在不断言的情况下参考它.建议?
它应该是:
有没有人知道一种方法,使派生类自动实例化一个模板类型的静态变量(这要么不需要派生类的编写器,或强迫他调用这个静态方法,以使派生类定义有效).
这可能是不可能理解的,我会尝试更好地定义它.
基本上我有一个全局工厂类,带有一个名为registerType的模板化函数.对于从Entity派生的每个类,我需要使用派生类型的模板参数调用此函数.目前,我必须在某个init函数中手动执行此操作,这会导致对此函数的大量调用,这违反了我的模板原则.
所以我有这个:
class Factory
{
template <typename EntityType>
registerEntityType();
};
void someInitFunction()
{
/// All of these are derived from Entity
gFactory.registerEntityType<EntityType1>();
gFactory.registerEntityType<EntityType2>();
gFactory.registerEntityType<EntityType3>();
/// and so on
}
Run Code Online (Sandbox Code Playgroud)
而我宁愿这样:
class Factory
{
template <typename EntityType>
registerEntityType();
};
class Entity // Abstract
{
/// This function should be called automatically with the derived
/// type as a parameter
SomeStaticConstructor<MDerivedType>()
{
gFactory.registerEntityType<MDerivedType>();
}
};
Run Code Online (Sandbox Code Playgroud)
编辑:这是不起作用的静态重复模板代码:
这是我的基类,以及用于自动注册东西的类
template <typename DerivedType>
class Registrar
{
public:
Registrar();
void check();
};
template <typename …Run Code Online (Sandbox Code Playgroud) 如何仅在特定类中限制类的实例化?
我不想在单个文件中限制它,因此匿名命名空间不适合我.
请注意,我想让整个世界都可以看到要限制的类的声明,只是来自整个世界的一个特定候选者只能实例化它.
我怎样才能做到这一点?
我想要这几次,并且无法想出一个体面的方法来做到这一点.
假设我在A类中有一个成员函数.我希望能够从一个不相关的B类调用该函数,但通常不能调用.您可能会说,"当然,将该功能设为私有,并宣布B为A的朋友." 这就是我一直在做的事情,但似乎有点矫枉过正.我真的不想让B访问A中的所有内容,只需要一个函数.
简而言之:A::func()只能由B级调用,但B不是A的朋友.可能吗?
有没有办法伪造它?
是否有任何可以模拟最接近行为的C++宏/模板/魔术?
Util.h (图书馆)
class Util{
//note: by design, this Util is useful only for B and C
//Other classes should not even see "Util"
public: static void calculate(); //implementation in Util.cpp
};
Run Code Online (Sandbox Code Playgroud)
Bh (图书馆)
#include "Util.h"
class B{ /* ... complex thing */ };
Run Code Online (Sandbox Code Playgroud)
Ch (图书馆)
#include "Util.h"
class C{ /* ... complex thing */ };
Run Code Online (Sandbox Code Playgroud)
Dh (用户)
#include "B.h" //<--- Purpose of #include is to access "B", but not "Util"
class D{
public: static …Run Code Online (Sandbox Code Playgroud) 我有class A和class B.我想class A访问其中一个class B私有函数; 但只有这一点,而不是其他一切.那可能吗?
某种例子:
class A {
//stuff
};
class B {
int r; // A cant use this
MagicFriendKeyword A void func(); // A can use this
public:
...
};
Run Code Online (Sandbox Code Playgroud)