C++函数中局部类的使用 [英] Usage of local class in C++ function

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

问题描述

我在 C++ 函数中看到了一些内部结构的用法.

有一个通用接口IBase.这是代码草案.

class IBase{虚拟方法()=0;}向量<IBase*>基本列表;

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

void func(){结构对象:公共 IBase{虚方法(){//func中对象的方法}}IBase* base = new Object();baseList->push(base);}

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

其他线程可能使用这个 baseList 来处理传入的事件.

struct Object"的内部结构的范围是什么?这很有趣.有没有关于这个的文件?

解决方案

struct Object"内部结构的作用域是什么?

局部类的作用域是定义它们的函数.但这本身并不有趣.

本地类的有趣之处在于如果它们实现了一些接口(就像你的代码那样),那么你可以创建它的实例(使用new)并返回它们(例如,作为 std::vector),从而使实现可以通过基类指针访问,即使在功能.

关于本地课程的其他一些事实:

  • 它们不能定义静态成员变量.

  • 他们不能访问封闭函数的非静态自动"局部变量.但是他们可以访问 static 变量.

  • 它们可以在模板函数中使用.

  • 如果它们是在模板函数中定义的,那么它们可以使用封闭函数的模板参数.

  • 本地类是最终的,这意味着函数之外的用户不能从本地类派生到函数.如果没有本地类,您必须在单独的翻译单元中添加一个未命名的命名空间.

  • 本地类用于创建蹦床功能,通常称为重击.

<小时>

编辑

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

9.8 本地类声明 [class.local]

<块引用>

1.类可以在函数定义中定义;这样的班级是称为本地类.一个名字本地类是其封闭类的本地类范围.本地类在范围内的封闭范围,并具有对外部名称的相同访问功能与封闭功能.本地声明类只能使用类型名称,静态变量、外部变量和函数和枚举器封闭范围.

[示例:整数 x;无效 f(){静态整数;整数 x;extern int g();结构本地{int g() { 返回 x;}//错误:x 是自动的int h() { 返回 s;}//行int k() { 返回::x;}//行int l() { 返回 g();}//行};//...}本地* p = 0;//错误:本地不在范围内—结束示例]

<块引用>

2.封闭函数没有对本地成员的特殊访问班级;它遵守通常的访问规则(第 11 条).成员函数本地类应定义在他们的类定义,如果他们是完全定义.

3.如果类 X 是本地类,则可以在其中声明嵌套类 YX 类及以后定义在X 类的定义或稍后定义在相同的范围内X 类的定义. 嵌套的类在本地类中是本地类.

4.本地类不应有静态数据成员.

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

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

class IBase
{
    virtual Method()=0;
}

vector<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.

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

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.

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.

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

  • They can be used in template functions.

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

  • 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

Some references from the Standard (2003)

9.8 Local class declarations [class.local]

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. 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. 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. A local class shall not have static data members.

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

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