I need a few helps for some things that I just cannot get the syntax of! (I need these, and if you don't believe me I can post the reason)
1) Creating a function within a function
2) Creating a class within a function
3) Declaring a template function outside a class
EXAMPLES:
1)

typedef void(*FUNC)();
FUNC GenFunc()
{
     FUNC ret=;//HELP! I NEED TO MAKE A FUNCTION
     return ret;
}

2)

class Abstract
{
    public:
    virtual void do()=0;
}
Abstract &getClass()
{
    Abstract &ret=;//HELP! I NEED TO MAKE A CLASS DERIVED FROM Abstract
    return ret;
}

3)

class Thing
{
    public:
    template <typename T>
    T thingy();
}
//NOW WHAT? HOW DO I WRITE THE CODE OF thingy?!

Answers to any of these questions would be greatly appreciated, thanks!

Recommended Answers

All 4 Replies

Let me take this in the reverse order:

3) Declaring a template function outside a class
That's easy, the syntax is as follows:

class Thing
{
    public:
    template <typename T>
    T thingy();
}

template <typename T>
T Thing::thingy() {
  //blablabla...
};

If Thing is also a template:

template <typename T>
class Thing {
  public:
    template <typename U>
    U thingy();
};

template <typename T, typename U>
U Thing<T>::thingy() {
  //blablabla...
};

2) Creating a class within a function
This is allowed in C++, but fairly rare. The syntax is also very straight-forward:

class Abstract
{
    public:
    virtual void do()=0;
}
Abstract* getClass()
{
    class Derived : public Abstract {
      //blablabla...
    };
    Abstract* ret= new Derived(); //this is very ugly, but allowed.
    return ret;
}

EDIT: Thanks to firstPerson for pointing out a little mistake I made about the not returning a reference to a local variable of class Derived. In this case, you would need to allocate the function dynamically and return a polymorphic pointer (or better yet, a smart-pointer).

But there are special rules about such classes, for instance, they cannot be class templates or have function templates in them. I think that you also cannot use any of the variable of the local scope of the enclosing function in the class. Etc. etc. You can read up on it by googling.

1) Creating a function within a function
This is not (currently) allowed in C++. Of course, you can have static member functions to a class which is declared in the body of the function, which basically gives you the same result. However, you cannot (currently) use the concept of "closures" which is this idea, like in other languages like Pascal and FORTRAN, that you could define the function in a function, and that nested function, once called, has access to the local variables of its enclosing function. Forbidding closures is a design choice that was made a long time ago (in C) for a somewhat obscure reason that I cannot recall.

Luckily, the upcoming standard, C++0x, will have a mechanism called Lambda expressions (which also supports closures) that would serve your needs and more:

//this will be a new way to define any type of callable thing (function pointer, functor, lambda expression, etc.):
typedef std::function<void()> FUNC; 

FUNC GenFunc()
{
     FUNC ret = []() { /* blablabla.. */ }; 
     return ret;
}

You can read up on lambda expressions on wikipedia.

For you example #2, isn't that undefined? You are( or will be) referencing a variable that is out of scope.

I have one quick question then. Could you create a class inside a function, define its functions then return the class to access its functions? Also when does C++0x come out and how do we get it?

>>Could you create a class inside a function, define its functions then return the class to access its functions?

Humm, you can't return a class, only an object (via a pointer). A class inside a function is called a local class. A local class has no linkage. This means that its functions cannot be called from outside the function that encloses the local class. I guess, you should still be able to call virtual functions from outside the enclosing function, provided that these are virtual functions of a base class that is not itself a local class (duh!) (and, frankly, I don't even know if that would even work because I've never tried and never seen any code that does that, even plain local classes are so rare that many people don't know about them at all). Basically, a local class does not exist outside the body of the function (literally, since it has no linkage).

>>Also when does C++0x come out?

I believe the plan is for it to come out before the end of this year, but the original plan was for 2008, so I wouldn't be so sure that it will be out by this year.

>> and how do we get it?

You can get it already on most recent compilers. Of course, it is not the official new standard because it hasn't come out, it's the draft version (which is expected to be pretty much the same thing). If you use GCC, here is a page that lists the main features of C++0x along with the version of GCC that supports it. Basically, I use GCC 4.6.0 which I compile from source (which you can get from the GNU-GCC website), and it is pretty good for supporting C++0x features, although I have experienced a few seg-faults since this is an experimental version (not released yet). But I think that version 4.5 is not so bad either. To enable C++0x, use the flag "-std=c++0x" or "-std=gnu++0x" (for GNU extensions). On windows' side, I think MSVC 2010 has pretty decent support too. And, as always, Comeau is pretty good too.

When the standard comes out, all compilers will have to turn ON C++0x as the default and will have to support its features. That could take some time for some compiler makers. Basically, just making sure you always have the latest version is all you need to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.