English 中文(简体)
在接口中与反应堆做什么
原标题:what to do with the destructors in an interface

在我撰写C++接口课程时,我选择了以下两个选项中的任何一个。

class Interface
{
public:
   virtual R1 f1(p11, p12 , ...) = 0;
   ...
   virtual Rn fn(pn1, pn2 , ...) = 0;
   virtual ~Interface() {} 
}

class Interface
{
public:
   virtual R1 f1(p11, p12 , ...) = 0;
   ...
   virtual Rn fn(pn1, pn2 , ...) = 0;
   virtual ~Interface() = 0; 
}
Interface::~Interface() {}

The first version is sh或ter to write
The second is attractive in that all functions of the interface are pure virtual

Is there any reason I should prefer one 或 the other method (或 perhaps a third one)?
Thanks

最佳回答

如我所知,使虚拟功能事实上<>>>的目的,是迫使衍生类别要么为之提供实施,要么选择通过 /explicitly written Base:f( in the Derived:f(>

因此,如果情况确实如此,那么制造虚拟脱轨器的用途是什么? 这是否迫使衍生的类别为<代码>Base:~Base()提供实施? 衍生课程可以实施<代码>Base:~Base(? 页: 1

因此,这意味着第1版有<条码>虚拟,轨道似乎足以达到 几乎<>。 毕竟,虚拟脱轨器的最常见目的是,客户可以通过<代码>Base*的电线器对衍生产品进行正确编码

However, if you make all functions in Base virtual only, not pure virtual, and provide implementations for them (actually you ve to provide), and at the same time you want to make Base abstract type, then having a pure virtual destructor in Base is the only solution:

class Base
{
public:
   virtual void f() {}; //not pure virtual
   virtual ~Base() = 0; //pure - makes Base abstract type!
};

Base::~Base() {} //yes, you have to do this as well.

Base *pBase = new Base(); // error - cannot create instance!

希望会有所帮助。

问题回答

对我来说,拖拉机不是接口的一部分。 信托局将采用其他语文,而不是复制人。 同样,你可以写给信托人之前和之后的条件,但不能写给经纪人。 这使得它只是一个C++的战线,第一个技术是解决这一问题的最舒适方式。

Okay发现了一个联系点,因此,我想把它作为答案:

网上虚拟功能是否真是非专利?

I ve seen compilers that don t emit any v-table if no non-inline function at all exists (and defined in one implementation file instead of a header then). They would throw errors like missing vtable-for-class-A or something similar, and you would be confused as hell, as i was.

Indeed, that s not conformant with the Standard, but it happens so consider putting at least one virtual function not in the header (if only the virtual destructor), so that the compiler could emit a vtable for the class at that place. I know it happens with some versions of gcc. (Johannes Schaub)

这与你的第二起案件略有不同(事实上,为了不成为受害人,总书记员实际上完全接管了该职务),但我认为我要提到这一点。 克谢克尔有时会ite。

In the first case, the derived class may choose whether or not to implement a destructor. In the second case, the pure virtual destructor must be overridden, so the derived class is forced to implement a destructor.

除非你有某种理由想强行这样做,否则我会处理第一起案件。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签