在C ++函数中使用本地类 [英] Usage of local class in C++ function
问题描述
我在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>
-
他们不能定义静态成员变量。
Some other facts about local classes:
他们不能访问封闭函数的非静态自动局部变量。但是他们可以访问
static
变量。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.
本地类是final,这意味着该函数之外的用户不能从本地派生到函数中。没有本地课程,您必须在单独的翻译单元中添加一个未命名的命名空间。
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.
编辑
标准(2003)
\1。一个类可以在函数定义中定义;这样一个类是
,称为本地类。
本地类的名称是其封闭的
范围的本地类。本地类位于封闭范围的
的范围内,
与
函数之外的
名称相同。本地
类中的声明只能使用类型名称,静态
变量,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
定义的类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屋!