Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 950
giggle Avatar asked Apr 19 '11 09:04

giggle


People also ask

What is the use of local class?

Local classes are similar to inner classes because they cannot define or declare any static members. Local classes in static methods, such as the class PhoneNumber , which is defined in the static method validatePhoneNumber , can only refer to static members of the enclosing class.

What are local classes in C?

A class declared inside a function becomes local to that function and is called Local Class in C++. A local class name can only be used locally i.e., inside the function and not outside it. The methods of a local class must be defined inside it only. A local class can have static functions but, not static data members.

What is local class with example?

Local Class in C++A class declared inside a function is known as a local class in C++ as it is local to that function. An example of a local class is given as follows. In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as a local class.

What is the use of local class in C++?

A local class is declared within a function definition. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions.


2 Answers

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.

like image 184
Nawaz Avatar answered Oct 04 '22 06:10

Nawaz


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

BUT you can do this, inside of a local class

int GetCount() {     class _local     {     public:         static int Count(int count = std::numeric_limits<int>::max())         {             static int count_ = 0;             if (count != std::numeric_limits<int>::max()) count_ = count;             return count_;         }          static float Operation(float  a, float  b)         {             _local::Count(_local::Count() + 1);             return a;         }     };    _local::Count(0);    CALLBACK( _local::Operation);    return _local::Count(); } 

_local::Count can be used to read and write the otherwise static variable

-aydin

like image 20
aydin Avatar answered Oct 04 '22 05:10

aydin