在C ++函数中使用本地类 [英] Usage of local class in C++ function

查看:77
本文介绍了在C ++函数中使用本地类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++函数中看到了一些内部结构的使用。

I see some usage of internal struct in c++ function.

有一个常见的接口IBase。这是代码草案。

There is a common interface IBase. Here is the draft code.

class IBase
{
    virtual Method()=0;
}

vector<IBase*> baseList;

然后,一个函数根据该IBase定义一个内部类,然后将内部类对象推入baseList。

Then a function defined an internal class based on that IBase, and then push the internal class object into the baseList.

void func()
{
    struct Object : public IBase
    {
        virtual Method()
        {
            // Method of Object in func
        }
    }

    IBase* base = new Object();
    baseList->push(base);

}

这似乎是一个奇怪的用法,但是一个很好的消息实现/事件创建模式。

It seems a strange usage, but a nice implementation of message/event creation pattern.

其他线程可能使用此baseList来处理传入的事件。

Other threads maybe use this baseList to handle the incoming event.

的struct Object的内部结构?这很有趣有没有一些文件谈论这个?

What's the scope of internal struct of "struct Object"? It's very interesting. Is there some documents talking about this?

推荐答案


struct Object的内部结构的范围是什么?

What's the scope of internal struct of "struct Object"?

本地类的范围是定义它们的函数。但这本身并不有趣。

The scope of the local classes is the function in which they're defined.But that isn't interesting in itself.

让本地课程感兴趣的是如果他们实现了一些界面(像你的代码一样),那么你可以创建它的实例 new )并返回它们(例如,作为 std :: vector< IBase *> ), em>实现可以通过基类指针访问。

What makes local classes interesting is that if they implement some interface (like your code does), then you can create instances of it (using new) and return them (for example, as std::vector<IBase*>), thereby making the implementation accessible through the base class pointer even outside the function.

有关本地类的其他一些事实: p>

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