Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

design patterns - Usage of local class in C++ function

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...