C++ can we have functions inside functions?

I mean like

int main(){
  void a(){
  //code
  }
  a();
}

AFAIS: nested classes and structs are most common solution...

Answers


No, C++ doesn't support that.

That said, you can have local classes, and they can have functions (non-static or static), so you can get this to some extend, albeit it's a bit of a kludge:

int main() // it's int, dammit!
{
  struct X { // struct's as good as class
    static void a()
    {
    }
  };

  X::a();

  return 0;
}

However, I'd question the praxis. Everyone knows (well, now that you do, anyway :)) C++ doesn't support local functions, so they are used to not having them. They are not used, however, to that kludge. I would spend quite a while on this code to make sure it's really only there to allow local functions. Not good.


C++11 supports this using lambdas/closures.

int main() {
    auto f = []() { return 42; };
    std::cout << "f() = " << f() << std::endl;
}

In recent versions of GCC and clang you can test this by passing the -std=c++11 flag to the compiler.


Need Your Help

C#:ZedGraph Zooming all panes simultaneously

c# zedgraph

I am using zedgraph to plot my data. I have a MasterPane and it has three GraphPanes. After drawing data I want to zoom all graphs simultaneously. Any suggestion?

Jquery Ajax twitter/facebook profile images and http 302

jquery ajax facebook twitter http-status-code-302

So, I've read a couple things about jquery ajax and http 302 both here on stack overflow and other sites. Some places seem to say that it's an unfixable problem, others provide solutions that don't...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.