限制基类可见性 [英] Restricting base class visibility

查看:74
本文介绍了限制基类可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统看起来像这样:


class AbstractBase {/ * ... * /};

template< class T> class Impl:public AbstractBase {/ * ... * /};

// ...

Impl< int>我;

Impl< float> f;

std :: vector< AbstractBase *> vec;

vec.push_back(& i);

vec.push_back(& f);

// ...


它有效。但是,目前,AbstractBase类具有公共

可见性 - 任何人都可以将其子类化。理想情况下,

AbstractBase的唯一子类应该是Impl的实例化。另一方面,Impl,

可以(并且应该)可由任何人实例化。是否有可能以某种方式隐藏AbstractBase以限制哪些类可以将其子类化?


谢谢,

Rennie deGraaf

I have a system that looks like this:

class AbstractBase { /* ... */ };
template <class T> class Impl : public AbstractBase { /* ... */ };
// ...
Impl<int> i;
Impl<float> f;
std::vector<AbstractBase*> vec;
vec.push_back(&i);
vec.push_back(&f);
// ...

It works. However, at the moment, class AbstractBase has public
visibility - anyone can subclass it. Ideally, the only subclasses of
AbstractBase should be instantiations of Impl. Impl, on the other hand,
can (and should) be instantiable by anyone. Is it possible for me to
somehow hide AbstractBase to restrict what classes can subclass it?

Thanks,
Rennie deGraaf

推荐答案

Rennie deGraaf写道:
Rennie deGraaf wrote:
我的系统看起来像这样:

类AbstractBase {/ * ... * /};
模板< class T> class Impl:public AbstractBase {/ * ... * /};
// ...
Impl< int>我;
Impl< float> f;
std :: vector< AbstractBase *> vec;
vec.push_back(& i);
vec.push_back(& f);
// ...

它有效。但是,目前,AbstractBase类具有公共可见性 - 任何人都可以将其子类化。理想情况下,AbstractBase的唯一子类应该是Impl的实例化。另一方面,Impl可以(而且应该)由任何人实例化。是否有可能以某种方式隐藏AbstractBase以限制哪些类可以将其子类化?
I have a system that looks like this:

class AbstractBase { /* ... */ };
template <class T> class Impl : public AbstractBase { /* ... */ };
// ...
Impl<int> i;
Impl<float> f;
std::vector<AbstractBase*> vec;
vec.push_back(&i);
vec.push_back(&f);
// ...

It works. However, at the moment, class AbstractBase has public
visibility - anyone can subclass it. Ideally, the only subclasses of
AbstractBase should be instantiations of Impl. Impl, on the other hand,
can (and should) be instantiable by anyone. Is it possible for me to
somehow hide AbstractBase to restrict what classes can subclass it?



您可以将AbstractBase的构造函数设为私有,然后使Impl成为

AbstractBase的朋友。这样,只有Impl知道如何构造

的AbstractBase子对象。


但我很好奇,为什么要限制在这个中使用AbstractBase方式?


希望这会有所帮助,

-shez-


You can make the constructor of AbstractBase private, then make Impl a
friend of AbstractBase. That way, only Impl knows how to construct the
AbstractBase sub-object.

But I''m curious, why restrict the use of AbstractBase in this way?

Hope this helps,
-shez-




Rennie deGraaf写道:

Rennie deGraaf wrote:
我的系统看起来像这样:

类AbstractBase {/ * ... * /};
template< class T> class Impl:public AbstractBase {/ * ... * /};
// ...
Impl< int>我;
Impl< float> f;
std :: vector< AbstractBase *> vec;
vec.push_back(& i);
vec.push_back(& f);
// ...

它有效。但是,目前,AbstractBase类具有公共可见性 - 任何人都可以将其子类化。理想情况下,AbstractBase的唯一子类应该是Impl的实例化。另一方面,Impl可以(而且应该)由任何人实例化。是否有可能以某种方式隐藏AbstractBase以限制哪些类可以将其子类化?
I have a system that looks like this:

class AbstractBase { /* ... */ };
template <class T> class Impl : public AbstractBase { /* ... */ };
// ...
Impl<int> i;
Impl<float> f;
std::vector<AbstractBase*> vec;
vec.push_back(&i);
vec.push_back(&f);
// ...

It works. However, at the moment, class AbstractBase has public
visibility - anyone can subclass it. Ideally, the only subclasses of
AbstractBase should be instantiations of Impl. Impl, on the other hand,
can (and should) be instantiable by anyone. Is it possible for me to
somehow hide AbstractBase to restrict what classes can subclass it?




显而易见的问题是为什么AbstractBase和Impl是不同的

类开始,如果AbstractBase只有一个

子类。


Greg



The obvious question to ask is why are AbstractBase and Impl distinct
classes to start with, if AbstractBase is meant to have only a single
subclass.

Greg


Greg写道:
显而易见的问题是,为什么AbstractBase和Impl不同的类开始,如果AbstractBase是为了只有一个
子类。


从OP的代码中,看起来他想创建一个包含异构对象的向量
The obvious question to ask is why are AbstractBase and Impl distinct
classes to start with, if AbstractBase is meant to have only a single
subclass.
From the OP''s code, it looks like he wants to create a vector



。他的方法的问题是

< AbstractBase *>向量的参数不保留值

语义。更好的方法是使用boost :: any或

boost :: variant,保持值语义。


-shez-


containing heterogenous objects. The problem with his approach is that
the <AbstractBase*> parameter for vector does not preserve value
semantics. A better approach would be to use something boost::any or
boost::variant, where value-semantics are maintained.

-shez-


这篇关于限制基类可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆