模式回顾C ++:使用带有纯虚方法的结构代替接口 [英] Pattern review C++: using structs with pure virtual methods in place of interfaces

查看:63
本文介绍了模式回顾C ++:使用带有纯虚方法的结构代替接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习C ++,来自C#和Java背景。

我喜欢在我的代码中使用接口。在C ++中,虽然它们不存在,所以我用纯虚方法制作结构。



我的问题是:



1)根据我的理解,除了默认访问器之外,struct和class之间没有区别,但是当类的大小较小时,通常会看到struct被使用。因此,我决定在编写接口时使用struct作为约定,因为程序员查看struct的方式更接近于接口。关于这个问题是否还有其他约定,或者我的约定是否错误或者可能以任何方式引起混淆?



2)因为接口只有纯虚拟并且没有提供实现(大多数情况下,默认方法现在都是Java中的东西)我认为只为它创建一个头文件是合乎逻辑的,而不是同时生成.cpp和.h文件。因为.cpp文件应该具有实现,并且.h文件应该具有方法原型。再说一遍,关于这个问题还有其他约定,或者我的约定不好或者可能以任何方式引起混淆?



例如:



ISayHello.h

  struct  IRender 
{
virtual void render()= 0 ;
}





ImplementationExample.cpp

  class 精灵: public  IRender 
{
public
void render()覆盖
{
// 代码在这里
}
}





我的尝试:



注意:我的意图是获得那些对我的模式如何工作有经验的人的意见,如果有问题,以及我的公约正在解决的问题是否有任何其他公约。如果您认为这个问题不合适,我将不胜感激,如果您可以将我重定向到一个更好的地方来提问我。例如,这个问题在代码审查堆栈交换中被标记为关闭主题,但在我看来,要求审查您的代码中的模式并非偏离主题。

解决方案

< blockquote> C ++中结构和类之间的主要区别是成员,结构的方法默认是公共的,默认情况下它们在类中是私有的。否则他们基本上是一回事。我没有看到你正在做什么的问题。



至于什么.h和.cpp文件是应该包含的 - 这可以非常一点点。有时相当多的实现将在头文件中,如模板代码或具有大量内联方法的类。我的一般经验法则是尽可能少地放在头文件中。它们是公共接口,模块之间共享的内容,我尽量将其保持在最低限度。例如,这些天我几乎从不在头文件中声明对话框类。我发现使用对话框的模块确实不需要细节。他们只需要一个接口函数,让他们传入和传出一些数据。这对我来说是简单的依赖关系,并且在对话框代码更改时最小化编译。


您的代码没有问题。



但是,在C ++中你可能需要考虑这些对象是如何被销毁的......所以在某些情况下,拥有一个虚拟的空析构函数可能是有意义的,但它不再是C#或Java中的接口了。



所以它会与其他语言类似,但在某些情况下你需要更多的预防措施。



特别是,由于C#和Java是垃圾收集的,通常,您不需要处理对象,因为运行时会在某些点自动检测到对象不再使用。



这将导致在C ++中,您可能必须能够以某种方式正确删除原始对象,而在托管语言中,您无需关心对象是否存在没有一个neede的资源d要关闭。


I just started to learn C++, coming from a C# and Java background.
I like to use interfaces in my code. In C++ though they do not exist so instead I make structs with pure virtual methods.

My questions are:

1) From what I understand there is no difference between struct and class besides the default accessor, but it is common place to see struct being used when the class is of a smaller size. Therefore I decided as a convention to use struct when writing interfaces since the way struct is viewed by programmers more closely resembles an interface. Are there any other conventions on this issue or is my convention bad or could cause confusion in any way?

2) Since an interface only has pure virtual and provides no implementation (most of the times, default methods are now a thing in Java) I thought it would be logical to only make a header file for it, instead of making both a .cpp and a .h file. Since a .cpp file is supposed to have the implementation and the .h file is supposed to have the method prototypes. Again, are there any other conventions on this issue or is my convention bad or could cause confusion in any way?

Example:

ISayHello.h

struct IRender
{
    virtual void render() = 0;
}



ImplementationExample.cpp

class Sprite : public IRender
{
public:
    void render() override
    {
        //code goes here
    }
}



What I have tried:

NOTE: My intention is to get the opinion of those with more experience on how my pattern works, if it has problems, and if there is any other convention on issue that my convention is addressing. If you think the question is off topic I would appreciate if you could redirect me to a better place to ask my question. This question for example was marked as off topic on the Code Review Stack Exchange, though in my opinion asking for review on a pattern you in your code is not off topic.

解决方案

The major difference between a struct and a class in C++ is members and methods of a struct are public by default and they are private by default in a class. Otherwise they are essentially the same thing. I don't see a problem with what you are doing.

As for what a .h and a .cpp file are "supposed" to contain - that can very a bit. Sometimes quite a bit of implementation will be in a header file like for template code or classes with lots of inline methods. My general rule of thumb is to put as little as possible in header files. They are the public interface, what is shared between modules, and I try to keep that to a minimum. For example, these days I virtually never declare dialog classes in header files. I find that modules that use the dialogs really don't need the details. All they need is an interface function that lets them pass some data in and out. This is simplified dependencies quite a bit for me and minimizes compilation when dialog code changes.


There are no problem with your code.

However, in C++ you might need to consider how those object get destroyed... so in some case, it might make sense to have a virtual empty destructor but it would not be an "interface" anymore as those in C# or Java.

So it would be similar to other languages but you would need a bit more precaution in some cases.

In particular, since C# and Java are garbage collected, often, you don't need to dispose your object as the run-time would automatically detect at some points that objects are not in use anymore.

It will result that in C++, there would be case where you would have to be able to properly delete the original object somehow while in managed language you won't need to care if the object does not hold a resource that needed to be closed.


这篇关于模式回顾C ++:使用带有纯虚方法的结构代替接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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