简体   繁体   English

用C ++创建更多OOP类

[英]Creating more OOP classes in C++

I have been working with QT and I noticed that they took OOP to another level by implementing some new features to classes such as private slots: public slots: signals: etc... What are they doing to declare such catagories of a class? 我一直在使用QT,我注意到他们通过在private slots: public slots: signals:等类中实现一些新功能来将OOP提升到另一个层次private slots: public slots: signals:等等...他们正在做什么来声明这类类的类别? Is it compiler specific or is it simply a sort of typedef ? 它是编译器特定的还是仅仅是一种typedef I am guessing it's portable to major OS's since QT will run on several systems. 我猜它可以移植到主要操作系统,因为QT将在几个系统上运行。 I ask out of curiosity and to create my own subclasses to help organize and create more OOP programs. 我出于好奇并创建自己的子类来帮助组织和创建更多的OOP程序。 For example 例如

class Main
{
handles:
  HANDLE hWin;
threads:
  HANDLE hThread;
};

And then clean inheritance would be easy by simply doing 然后通过简单的操作就可以轻松实现干净的继承

class Dialog : handles Main
{

};

To me, it looks like people are not answering your question, they're answering something a little different. 对我来说,看起来人们没有回答你的问题,他们回答的是一些不同的东西。 You seem to be talking about the "section tags" that just happen to be used for slots, and would like section tags for your own class sections like handles and threads. 你似乎在谈论恰好用于插槽的“section标签”,并希望你自己的类部分的section标签,如句柄和线程。 That is something that QT does by preprocessing the code before it gets sent to the compiler. 这是QT通过在将代码发送到编译器之前预处理来完成的。 It's not something you could do yourself without adding another compilation stage. 如果不添加另一个编译阶段,这不是你自己可以做的事情。

That said, it really doesn't have much of a use, except to tell the QT precompiler the sections to find it's slots in. You can't inherit them, as you appear to want to do. 也就是说,除了告诉QT预编译器部分找到它的插槽之外,它确实没什么用处。你不能继承它们,就像你想要做的那样。 It just marks an area to generate introspective code on. 它只标记了一个生成内省代码的区域。

And you really wouldn't want to do it like that anyway. 而且你真的不想这样做。 If you have separate components that you'd like to inheret, separate them into separate classes, and then make those separate classes members of your larger class that controls their interaction. 如果您有自己想要的单独组件,请将它们分成不同的类,然后将这些单独的类成为控制其交互的较大类的成员。 If you tried to split apart a class that had different sections, there would be no way for the compiler to ensure that they didn't interact in some way that required invariants of the other pieces, because the compiler doesn't put those kinds of boundaries up on using the different members. 如果你试图拆分一个具有不同部分的类,编译器将无法确保它们不以某种方式进行交互,这需要其他部分的不变量,因为编译器不会放置那些类型的使用不同成员的界限。

It is called signals and slots is implemented by the Qt meta-object compiler moc . 它被称为信号和槽Qt元对象编译器moc实现 The moc also adds an introspection system to pre-processed classes. moc还为预处理类添加了内省系统。 There are also exist signal/slot implementations that don't require a special preprocessing step such as Boost.Signals and Boost.Signals2 . 还存在信号/插槽实现,其不需要特殊的预处理步骤,例如Boost.SignalsBoost.Signals2 I wouldn't consider signals and slots more or less OOP than normal message passing through function calls, but that is argumentative and not really relevant. 我不会比通过函数调用的正常消息更多或更少地考虑信号和插槽OOP,但这是有争议的并且不是真正相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM