Nested functions in C++

Discussion in 'C++' started by A, Sep 21, 2003.

  1. A

    A Guest

    Hi,

    How do you make use of nested functions in C++? I realize in C++ that
    everything must be declared first in a header file before implementation in
    a .cpp file. I tried to nest a method prototype in another prototype but
    seems pointless. Can someone please write a short, simple, and concise
    skeleton code of how to use nested functions?

    class Foo
    {
    private:
    int a;
    int b;

    public:
    void funcA();
    void funcB(); // seems pointless
    }

    void Foo:: funcA()
    {
    this->funcB;
    void Foo::funcB()
    {...}
    }
    //error: local method problem

    any help appreciated.

    Regards
    reuytrt


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003
     
    A, Sep 21, 2003
    #1
    1. Advertising

  2. A

    Dave Theese Guest

    "A" <> wrote in message
    news:...
    > Hi,
    >
    > How do you make use of nested functions in C++? I realize in C++ that
    > everything must be declared first in a header file before implementation

    in
    > a .cpp file. I tried to nest a method prototype in another prototype but
    > seems pointless. Can someone please write a short, simple, and concise
    > skeleton code of how to use nested functions?
    >
    > class Foo
    > {
    > private:
    > int a;
    > int b;
    >
    > public:
    > void funcA();
    > void funcB(); // seems pointless
    > }
    >
    > void Foo:: funcA()
    > {
    > this->funcB;
    > void Foo::funcB()
    > {...}
    > }
    > //error: local method problem
    >
    > any help appreciated.
    >
    > Regards
    > reuytrt
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003
    >
    >


    Functions may not be nested in C++. Nor is it *required* that declarations
    appear in a header file, but good design generally dictates that interface
    and implementation be separated.

    Hope this helps!
     
    Dave Theese, Sep 21, 2003
    #2
    1. Advertising

  3. A wrote:
    > Hi,
    >
    > How do you make use of nested functions in C++?


    you can't...

    -- Nuclear / the Lab --
     
    John Tsiombikas (Nuclear / the Lab), Sep 21, 2003
    #3
  4. On Sun, 21 Sep 2003 15:01:30 +0930, "A" <> wrote:

    >How do you make use of nested functions in C++?


    Before you can use them you must have them.

    C++ does not support nested function à la Pascal, but does support a
    limited form of local classes (nested in functions).

    You can use local classes to achieve logical nesting, but a member
    function of a local class doesn't have access to the arguments and
    local variables of the enclosing function unless you provide such
    access yourself, e.g. via reference constructor arguments.



    >I realize in C++ that everything must be declared first in a header
    >file before implementation in a .cpp file.


    That is incorrect; the C++ standard does not even mention files.


    >I tried to nest a method prototype in another prototype but
    >seems pointless. Can someone please write a short, simple, and concise
    >skeleton code of how to use nested functions?
    >
    >class Foo
    >{
    > private:
    > int a;
    > int b;
    >
    > public:
    > void funcA();
    > void funcB(); // seems pointless
    >}


    It is, indentation is not equal to logical nesting.




    >void Foo:: funcA()
    >{
    > this->funcB;
    > void Foo::funcB()
    > {...}
    >}
    >//error: local method problem
    >
    >any help appreciated.


    The best you can do is forget it, then investigate the issue anew
    in a few years time.
     
    Alf P. Steinbach, Sep 21, 2003
    #4
  5. A

    Ying Yang Guest

    "Dave Theese" <> wrote in message
    news:dRabb.558$La.517@fed1read02...
    >
    > "A" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > How do you make use of nested functions in C++? I realize in C++ that
    > > everything must be declared first in a header file before implementation

    > in
    > > a .cpp file. I tried to nest a method prototype in another prototype but
    > > seems pointless. Can someone please write a short, simple, and concise
    > > skeleton code of how to use nested functions?
    > >
    > > class Foo
    > > {
    > > private:
    > > int a;
    > > int b;
    > >
    > > public:
    > > void funcA();
    > > void funcB(); // seems pointless
    > > }
    > >
    > > void Foo:: funcA()
    > > {
    > > this->funcB;
    > > void Foo::funcB()
    > > {...}
    > > }
    > > //error: local method problem
    > >
    > > any help appreciated.
    > >
    > > Regards
    > > reuytrt

    >
    > Functions may not be nested in C++. Nor is it *required* that

    declarations
    > appear in a header file, but good design generally dictates that interface
    > and implementation be separated.
    >
    > Hope this helps!



    Surely there must be a solution to this problem. The reason i wanted to do
    this is because i want to be able to group related functions together. For
    example a function that calls another related function to solve a problem
    (this can be seen in indirect recursion calls).


    Regards
    ewrewrwer


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    Ying Yang, Sep 21, 2003
    #5
  6. On Sun, 21 Sep 2003 18:53:06 +0930, Ying Yang wrote:
    > Surely there must be a solution to this problem. The reason i wanted to do
    > this is because i want to be able to group related functions together. For


    This is exactly why C++ was invented - to group functions together into
    classes. Look at following (incorrect) code:

    int f(int i)
    {
    int j = i + 1;
    int a() // local function is invalid!
    {
    return j * i;
    };

    return a() + i;
    }

    it won't work, but you can use class instead:

    class f {
    const int& i; // variables shared between all ...
    int j; // ... functions grouped into class
    int ret; // return value
    int a() // "internal" function
    {
    return j * i;
    }
    public:
    f(const int& arg_i) : i(arg_i), j(i + 1)
    {
    ret = a() + i;
    }

    operator int() {return ret;}
    // bonus: alternate return type from function object
    operator std::string() {return "hi!";}
    };

    You may call your function object simply creating it:
    int r = f(3);

    you may also retrieve its "alternate" return value:
    std::string q = f(4);

    regards


    B.
     
    Bronek Kozicki, Sep 21, 2003
    #6
  7. Re: Re: Nested functions in C++

    Bronek Kozicki <> wrote:
    # Look at following (incorrect) code:

    # int f(int i)
    # {
    # int j = i + 1;
    # int a() // local function is invalid!
    # {
    # return j * i;
    # };
    #
    # return a() + i;
    # }

    # it won't work, but you can use class instead:

    Maybe, but this will not replace the local (or nested, as you like) functions.
    It just tries to help with the problem they deal with.

    Local functions have an access to the local environment of the parent function.
    You cannot achieve this in C++. To have local functions you would like also
    to have a mechanism of closures, as it is done for example in boost::phoenix.

    # You may call your function object simply creating it:
    # int r = f(3);

    # you may also retrieve its "alternate" return value:
    # std::string q = f(4);

    Fine, but you still have to pass arguments to such a "functionate".
    You have declared a local function in this (incorrect) code before,
    which does not need to be passed an argument; it takes the value from
    the local environment from the parent function. You cannot simulate
    this anyhow in C++ (unless you directly pass a "closure" as an argument :).

    Regards,


    --
    1 6 1 7 4 4 2 548 g4bc7a4 66z 3xt7w v1y z9p1 120 32
    (( Michal "Sektor" Malecki w4 66 64 73 7564 24 5 v 34 4
    )) ektor van Skijlen 1 5 5 1 844 a v r z 4
    Software engineer, Motorola GSG Poland 1 2 2a 1 4
    WARNING: Opinions presented by me on usenet groups are my personal opinions
    ONLY and are not connected to the employer.
     
    Sektor van Skijlen, Sep 23, 2003
    #7
  8. A

    michael.george.hart

    Joined:
    Oct 26, 2011
    Messages:
    1
    Here is your nested function/functor

    If you really want nested function, functor will do just a well, and the compiler can probably can do better optimization with them. Best of all you get to play friendship relationship.

    #include <iostream>
    int main(int argc, char **argv)
    {
    struct Function1
    {
    double operator()(double y)
    {
    return 3 * y;
    }
    };

    struct Function2
    {
    struct Nested
    {
    double operator()(int x)
    {
    return x + 1;
    }
    };

    double operator()(int y)
    {
    Nested nested;
    return 2 * nested(y);
    };
    };


    Function1 _1_level_nesting;

    Function2 _2_level_nesting;

    std::cout
    << "Hello, world!" << std::endl
    << "here is 1 levels nesting relative to main: " << _1_level_nesting(85) << std::endl
    << "here is 2 levels nesting relative to main: " << _2_level_nesting(10) << std::endl
    ;

    return 0;
    }
     
    Last edited: Oct 26, 2011
    michael.george.hart, Oct 26, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,187
    Russ Perry Jr
    Aug 20, 2004
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,629
    Victor Bazarov
    Jun 21, 2004
  3. Chad E. Dollins
    Replies:
    3
    Views:
    667
    Kai-Uwe Bux
    Nov 8, 2005
  4. request@no_spam.com
    Replies:
    5
    Views:
    442
  5. Ultrus
    Replies:
    3
    Views:
    390
    Stefan Behnel
    Jul 9, 2007
Loading...

Share This Page