Is it possible to have multiple layers of nested functions in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaemonCoder
    New Member
    • Mar 2008
    • 43

    Is it possible to have multiple layers of nested functions in c

    I am curious if its possible to create a structure for an small SDK smiliar to that of java for C/C++. IE a System.Out.prin tln() that prints a statment in c?

    I would guess i would have to use a struct to create system first but how would i nest the other structs beneath it, and could i still access the functions in the same manner.... System would have a Timer() function as well as Out witch is another Struct ... Is this possible.. If this has already been done please let me know.
    Code:
    typedef struct System {
              
                           typedef struct Out { 
                             void println();
    } System;
    
    }System.Out;    //  i know this is inproper ...  it is for example only...
    EDIT:
    Could i do the above if i did it as follows?
    [code=c]

    typedef struct System {
    } System;

    typedef struct Out System{
    void println();
    } Out;
    [/code]
    Last edited by DaemonCoder; Mar 9 '08, 06:00 PM. Reason: Added another Code Sample.. / added end code tag
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by DaemonCoder
    [code=c]

    typedef struct System {
    } System;

    typedef struct Out System{
    void println();
    } Out;
    [/code]
    Not quite, this sort of comes close

    [code=c]

    typedef struct Out{
    void (*println)(void );
    } Out;

    typedef struct System {
    Out out;
    } System;


    void actual_println( void)
    {
    }


    System system = {{actual_printl n}};
    [/code]
    Then you could call

    Code:
        system.out.println();
    This is C it would be a bit easier in C++ where classes can actually have function members;

    Comment

    • DaemonCoder
      New Member
      • Mar 2008
      • 43

      #3
      So i could do this in c++ like so?

      Code:
       
      using namespace std;
       
      class System {
       
      public:
      	System ();
      ~System (); 
      // do i have to define out here in some way?
       
      };
       
      class System::Out {
       
      	 public:
      		Out ();
      	 ~Out ();
      	  void println();
      }
       
      // and of course i have to define all the functions...
      This is very rough... Please point out my mistakes... Am i on the right track?
      EDIT:

      Sorry, I dont know why i put tthe tilde at the end had to fix that one LOL

      Comment

      • DaemonCoder
        New Member
        • Mar 2008
        • 43

        #4
        One more thing... How would i define this as a namespace to have a function call like so??
        Code:
         
        #include <customSystem.h>
         
        using namespace System::Out
         
        System::Out::println("hello"); // equivilant to printf but with no bios calls...for use in protected mode...
        EDIT: Used code tag CPP colors were way too intense. Changed to text.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by DaemonCoder
          Code:
           
          class System::Out {
          ...
          }
          This isn't valid C++ syntax but you are close, in C++ you have the additional advantage that you can declare classes (or structures) in classes (or structures), so you could do this

          [CODE=cpp]
          using namespace std;

          class System {

          public:
          System ();
          ~System ();

          class Out {
          public:
          Out ();
          ~Out ();
          void println();
          } out;
          };

          // and of course i have to define all the functions...
          [/CODE]

          Like this you would have to instantiate a object of type System

          [code=cpp]
          System system;
          system.out.prin tln();
          [/code]

          If you do not want to have instantiate an object then you could do it like this

          [CODE=cpp]
          using namespace std;

          class System {

          public:
          System ();
          ~System ();

          class Out {
          public:
          Out ();
          ~Out ();
          static void println();
          };
          };

          // and of course i have to define all the functions...
          [/CODE]
          Notice the static keyword in the println declaration

          then you could do this

          [code=cpp]
          System::Out::pr intln();
          [/code]

          If you not only do not want to have instantiate an object but you actually do not want to allow an object to be instantiated then you could do it like this

          [CODE=cpp]
          using namespace std;

          class System {

          private:
          System ();
          ~System ();

          public:
          class Out {
          private:
          Out ();
          ~Out ();
          public:
          static void println();
          };
          };

          // and of course i have to define all the functions...
          [/CODE]

          Note I have made all the constructors and destructors private to their classes.


          To use namespaces is quite easy it would be done like this

          [CODE=cpp]
          using namespace std;

          namespace System {

          namespace Out {
          static void println();
          };
          };

          // and of course i have to define all the functions...
          [/CODE]

          called like this

          [code=cpp]
          System::Out::pr intln();
          [/code]

          or

          [code=cpp]
          using namespace System::Out;
          println();
          [/code]

          But system is quite a generic namespace name, for instance it already exists in .NET

          Comment

          • DaemonCoder
            New Member
            • Mar 2008
            • 43

            #6
            Ok... So I spoke with my teacher and he says that C will be the hardest language to implement this in so he said to use C. This class is about Logic and i understand his reasons.. I still have a couple of questions though...

            Code:
             System system = {{actual_println}};
            I know that system is the base structure and we are creating an instantiation of it called system but what does the right had side of the equal mean...??

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by DaemonCoder
              Ok... So I spoke with my teacher and he says that C will be the hardest language to implement this in so he said to use C.
              lol, isn't that always the way with teachers.
              Originally posted by DaemonCoder
              Code:
               System system = {{actual_println}};
              I know that system is the base structure and we are creating an instantiation of it called system but what does the right had side of the equal mean...??
              OK this is structure initialisation the equivalent of
              Code:
                 int i = 6;
              for structures.
              The outer set of {...} are because System is a structure, I am saying I am initialising a structure. The first (and only)member System is a structure (Out) so there is another set of {...} to say I am initialising a sub-structure. Out contains a pointer to function member so I provide data to initialise this member (actual_println ).

              Taking a another eaxmple

              [code=c]
              typedef struct inner {
              int i1;
              int i2;
              } Inner;

              typedef struct outer {
              Inner inner
              long l1;
              } Outer;

              Outer outer = {{0, 1}, 2L};

              /* As an array */

              Outer array[2] = {
              {{0, 1}, 2L},
              {{2, 3}, 4L}
              };
              [/code]as you see arrays also a set of {...} in the initialisers to delimit them.

              Comment

              • DaemonCoder
                New Member
                • Mar 2008
                • 43

                #8
                [CODE=c]
                typedef struct Out {
                void (*println) (void);
                } Out;

                typedef struct In{
                void (*getline) (void);
                } In;

                typedef struct System {
                In in;
                Out out;
                } System;

                void println (void)
                {
                printf("system. out.println \n.");
                }
                void getline (void)
                {
                printf("\n system.in.getli ne ");
                }


                ...

                declaration code
                ... // error is in this general region dont know where i get a cascading error.

                System system = {{getline}print ln};

                system.out.prin tln();
                system.in.getli ne();

                [/CODE]

                I think i am doing this right... But i get an error... I am dumbfounded???

                Comment

                • DaemonCoder
                  New Member
                  • Mar 2008
                  • 43

                  #9
                  What did i do, and why does this obfuscated code work.?

                  Code:
                   
                   
                  typedef struct Out {
                   void (*println) (void);
                   
                  } Out;
                  typedef struct In{
                   void (*getline) (void);
                  } In;
                  typedef struct System {
                   In in; 
                   Out out;
                  } System;
                   
                  void println (void)
                  {
                   printf("\nsystem.out.println \n");
                  }
                  void getline (void)
                  {
                  printf("\nsystem.in.getline \n");
                  }
                  
                  System init()
                  { 
                  System system = {{getline}, println};
                  return system;
                  }
                   
                  int main()
                  {
                   System system = init();  
                   
                   system.out.println();
                   system.in.getline();

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by DaemonCoder
                    What did i do, and why does this obfuscated code work.?

                    Code:
                     
                    System system = {{getline}, println};
                    You added a , but println is part of the structure In so this should actually be
                    Code:
                     
                    System system = {{getline}, {println}};

                    Comment

                    • DaemonCoder
                      New Member
                      • Mar 2008
                      • 43

                      #11
                      Ok, I was mad at the computer cause the code did not work... And i made this function. I have no idea how it works? Can someone please explain this to me?



                      [CODE=c] System init()
                      {
                      System system = {{getline}, {println};
                      return system;
                      }

                      int main()
                      {
                      System system = init();
                      }
                      [/CODE]

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Originally posted by DaemonCoder
                        [CODE=c] System init()
                        {
                        System system = {{getline}, {println};
                        return system;
                        }

                        int main()
                        {
                        System system = init();
                        }
                        [/CODE]
                        I hope you realise that this code is in-efficient and fairly pointless as you could initialise system in main directly?

                        Anyway in init a structure system is created and initialised. This structure is returned from the function, using whatever method your platform uses to return structures, this tends to be platform dependent.

                        In main a structure system is created, the return data from init is then copied into the structure.

                        This involves 2 things both of which the compiler knows how to do

                        1. Returning a structure from a function.
                        2. Copying 1 structure to another structure (of the same type).

                        Note that returning structures from functions is normally considered bad practice.

                        Comment

                        • DaemonCoder
                          New Member
                          • Mar 2008
                          • 43

                          #13
                          Thank you Banfa. I will take into account that is is bad practice and remeber it for next time. As I previously stated before, I was mad at the computer so i threw some code at it. But now i realize my only mistake was a comma... I was tired and not thinking straight.

                          Well, Now that this project is finished.. Guess what language my teacher said to take the same program an write it over again in....?

                          Comment

                          Working...