抽象类的实现 [英] Implementation of abstract classes

查看:108
本文介绍了抽象类的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计。


我有一些来自某些基础的课程:


班级基础{

void foo();

void bar();

};


class a:public base {

};


b级:公共基地{

};


问题如下:


1)我想阻止用户制作

类基础的实例

2)有在基类

和派生类之间没有重载函数,所以我不能使用通常的


虚拟void overloaded_function()= 0;

基础班



据我所知,有两种方法可以继续:


1)制作一些虚拟虚拟函数,

派生类需要实现

2)将base的构造函数隐藏为受保护。


从语义的角度来看,后一种解决方案

似乎更优雅。这是一个很好的解决方案

或者是否有陷阱或障碍?


符文

Hi folks.

I have some classes which are derived from some base:

class base {
void foo();
void bar();
};

class a : public base {
};

class b: public base{
};

The problem is as follows:

1) I would like to prevent users from making instances of
class base
2) There are no overloaded functions beween the base class
and the derived classes, so I can not use the usual

virtual void overloaded_function() = 0;

in the base class.

As far as I can see, there are two ways to proceed:

1) make some virtual dummy function that the
derived classes need to implement
2) Hide the constructor of base as protected.

From a semantic point of view the latter solution
seems the more elegant. Is this a good solution
or are there traps or snags associated with it?

Rune

推荐答案

Rune Allnor写道:
Rune Allnor wrote:

1)我想阻止用户制作

类基础的实例

2)基类

和派生类之间没有重载函数,所以我不能使用通常的


virtual void overloaded_function()= 0;

基类中的

1) I would like to prevent users from making instances of
class base
2) There are no overloaded functions beween the base class
and the derived classes, so I can not use the usual

virtual void overloaded_function() = 0;

in the base class.



如果你的基类将以多态方式使用,你必须声明

它的析构函数虚拟,所以你不需要虚拟纯虚的

功能。只需声明析构函数本身是纯虚拟的:


virtual~base()= 0

-

Christian Hackl


Rune Allnor写道:
Rune Allnor wrote:

1)我想阻止用户制作
$ b的实例$ b class base

2)基类

和派生类之间没有重载函数,所以我不能使用通常的


virtual void overloaded_function()= 0;

基类中的



据我所知,那里有两种方法可以继续:


1)制作一些虚拟的虚拟函数,

派生类需要实现

2)将base的构造函数隐藏为protected。
1) I would like to prevent users from making instances of
class base
2) There are no overloaded functions beween the base class
and the derived classes, so I can not use the usual

virtual void overloaded_function() = 0;

in the base class.

As far as I can see, there are two ways to proceed:

1) make some virtual dummy function that the
derived classes need to implement
2) Hide the constructor of base as protected.



如果你的基类将以多态方式使用,你必须声明

它的析构函数虚拟,所以你不需要虚拟纯虚的

功能。只需声明析构函数本身是纯虚拟的:


virtual~base()= 0

-

Christian Hackl


9月20日18:40,Christian Hackl< ha ... @ sbox.tugraz.atwrote:
On 20 Sep, 18:40, Christian Hackl <ha...@sbox.tugraz.atwrote:

Rune Allnor写道:
Rune Allnor wrote:

1)我想阻止用户制作

* *类基础的实例

2)基类

* *和派生类之间没有重载函数,所以我不能使用通常的
1) I would like to prevent users from making instances of
* *class base
2) There are no overloaded functions beween the base class
* *and the derived classes, so I can not use the usual


* * virtual void overloaded_function()= 0;
* *virtual void overloaded_function() = 0;


* *在基类中。
* *in the base class.


据我所知,有两种方法可以继续:
As far as I can see, there are two ways to proceed:


1)制作一些虚拟虚函数,

* *派生类需要实现

2)隐藏base的构造函数作为protected。
1) make some virtual dummy function that the
* *derived classes need to implement
2) Hide the constructor of base as protected.



如果你的基类将以多态方式使用,你必须声明

它的析构函数虚拟,所以你不需要虚拟纯虚的

功能。只需声明析构函数本身是纯虚拟的:


virtual~base()= 0


If your base class is going to be used polymorphically, you must declare
its destructor virtual anyway, so you don''t need a "dummy" pure virtual
function. Just declare the destructor itself pure virtual:

virtual ~base() = 0



当然!


这个解决方案负责我现在正在处理

的情况,但如果有的话,怎么会这样做?/ b
清理在基类级别上做什么?


使用上面的解决方案,必须在每个中调用一些

base :: cleanup()函数衍生的

析构函数吧?


符文

Of course!

This solution takes care of the case I''m working on
right now, but how would one do this if there were
some cleaning up to do on the base class level?

With the above solution one would have to call some
base::cleanup() function in each of the derived
destructors, right?

Rune


这篇关于抽象类的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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