Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I define structures and classes within a function in C++?

I just mistakenly did something like this in C++, and it works. Why can I do this?

int main(int argc, char** argv) {     struct MyStruct     {       int somevalue;     };      MyStruct s;     s.somevalue = 5; } 

Now after doing this, I kind of remembered reading about this trick someplace, a long time ago, as a kind of poor-man's functional programming tool for C++, but I can't remember why this is valid, or where I read it.

Answers to either question are welcome!

Note: Although when writing the question I didn't get any references to this question, the current side-bar points it out so I'll put it here for reference, either way the question is different but might be useful.

like image 297
Robert Gould Avatar asked May 18 '09 02:05

Robert Gould


People also ask

Can you define a struct inside a function in C?

Yes, the standard allows this, and yes, the name you create this way is only visible inside the function (i.e., it has local scope, just like when you define int i; , i has local scope). or, if you're really only going to use it once, struct { /* ...

Can you define a struct in a function?

No, you can't. Structs can only contain variables inside, storing function pointers inside the struct can give you the desired result. Show activity on this post. No, but you can in c++ struct!

Can a class be defined inside a function?

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.

Can we define class with struct?

Yes you can. In c++, class and struct are kind of similar. We can define not only structure inside a class, but also a class inside one. It is called inner class.


2 Answers

[EDIT 18/4/2013]: Happily, the restriction mentioned below has been lifted in C++11, so locally defined classes are useful after all! Thanks to commenter bamboon.

The ability to define classes locally would make creating custom functors (classes with an operator()(), e.g. comparison functions for passing to std::sort() or "loop bodies" to be used with std::for_each()) much more convenient.

Unfortunately, C++ forbids using locally-defined classes with templates, as they have no linkage. Since most applications of functors involve template types that are templated on the functor type, locally defined classes can't be used for this -- you must define them outside the function. :(

[EDIT 1/11/2009]

The relevant quote from the standard is:

14.3.1/2: .A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

like image 200
j_random_hacker Avatar answered Oct 13 '22 21:10

j_random_hacker


One application of locally-defined C++ classes is in Factory design pattern:

 // In some header class Base { public:     virtual ~Base() {}     virtual void DoStuff() = 0; };  Base* CreateBase( const Param& );  // in some .cpp file Base* CreateBase( const Params& p ) {     struct Impl: Base     {         virtual void DoStuff() { ... }     };      ...     return new Impl; }  

Though you can do the same with anonymous namespace.

like image 40
Nikolai Fetissov Avatar answered Oct 13 '22 21:10

Nikolai Fetissov