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

查看:88
本文介绍了在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?

推荐答案


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 ),

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.

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

Some other facts about local classes:


  • 他们不能定义静态成员变量。

  • They cannot define static member variables.

访问非静态自动局部变量的封闭函数。但它们可以访问 static 变量。

They cannot access nonstatic "automatic" local variables of the enclosing function. But they can access the static variables.

如果在模板函数中定义它们,则可以使用封闭函数的模板参数。

If they are defined inside a template function, then they can use the template parameters of the enclosing function.

本地类是final,意味着函数之外的用户不能从本地类派生函数。没有本地类,您必须在单独的翻译单元中添加未命名的命名空间。

Local classes are final, that means users outside the function cannot derive from local class to function. Without local classes, you'd have to add an unnamed namespace in separate translation unit.

本地类用于创建(通常称为

Local classes are used to create trampoline functions usually known as thunks.

EDIT

来自标准(2003)的一些参考

Some references from the Standard (2003)


\1。类可以在函数定义中定义;这样的一个类是
,叫做局部类。
本地类的名​​称对于其包围的
范围是本地的。本地类在封闭范围的
范围内,并且

函数外部的名称的访问与包含
函数的访问相同。在本地
类中的声明只能使用类型名,static
变量,extern变量和
函数,以及来自
包含范围的枚举器。

\1. A class can be defined within a function definition; such a class is called a local class. The name of a local class is local to its enclosing scope. The local class is in the scope of the enclosing scope, and has the same access to names outside the function as does the enclosing function. Declarations in a local class can use only type names, static variables, extern variables and functions, and enumerators from the enclosing scope.



[Example:

int x;
void f()
{
   static int s ;
   int x;
   extern int g();

   struct local {
      int g() { return x; } // error: x is auto
      int h() { return s; } // OK
      int k() { return ::x; } // OK
      int l() { return g(); } // OK
   };
// ...
}
local* p = 0; // error: local not in scope

—end example]




\2。封装函数没有对本地
类成员的特殊访问权限;它遵守通常的访问规则
(第11条)。
本地类的成员函数在
中定义它们的类定义,如果它们是
定义的话。

\2. An enclosing function has no special access to members of the local class; it obeys the usual access rules (clause 11). Member functions of a local class shall be defined within their class definition, if they are defined at all.

3。如果类X是一个局部类,则嵌套类Y可以在
类X中声明,稍后在类X的
定义中定义或稍后
在与$ b相同的范围中定义$ b定义类X.在本地类中嵌套
的类是本地类。

\3. If class X is a local class a nested class Y may be declared in class X and later defined in the definition of class X or be later defined in the same scope as the definition of class X. A class nested within a local class is a local class.

\4。一个本地类不能有静态数据成员。

\4. A local class shall not have static data members.

这篇关于在C ++函数中使用本地类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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