Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to call 200 functions with 1 variable?

How to call 200 functions with 1 variable?

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 7 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by MasterBlade
    #1

    Hello I need to write 200 different functions. More importantly I need to name or mark them properly so I can call them with 1 variable (i).

    For example, if i has a value of 100 then I should call #100 function.

    I tried

    void (*a)[200]();
    

    but it won't compile. Is it possible to use Vector or something?

    Thanks for the help!

    jsulmJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What are these 200 functions ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • M MasterBlade

        Hello I need to write 200 different functions. More importantly I need to name or mark them properly so I can call them with 1 variable (i).

        For example, if i has a value of 100 then I should call #100 function.

        I tried

        void (*a)[200]();
        

        but it won't compile. Is it possible to use Vector or something?

        Thanks for the help!

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @MasterBlade Why do you need 200(!) functions? Or do you mean you want to call one using an index?
        If so then do:

        void f1() {}
        typedef void(*Function)();
        Function functions[200];
        functions[0] = &f1;
        ...
        // call one
        functions[i]();
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        4
        • SGaistS SGaist

          Hi,

          What are these 200 functions ?

          M Offline
          M Offline
          MasterBlade
          wrote on last edited by MasterBlade
          #4

          @SGaist said in How to call 200 functions with 1 variable?:

          Hi,

          What are these 200 functions ?

          @jsulm said in How to call 200 functions with 1 variable?:

          @MasterBlade Why do you need 200(!) functions? Or do you mean you want to call one using an index?

          I'm designing a card game. It currently has about 200 cards. There needs to be 1 function for each of them to do something.

          I think it should be possible to call the corresponding function with only the card #

          jsulmJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Are these 200 functions really doing something very different ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            0
            • SGaistS SGaist

              Are these 200 functions really doing something very different ?

              M Offline
              M Offline
              MasterBlade
              wrote on last edited by
              #6

              @SGaist said in How to call 200 functions with 1 variable?:

              Are these 200 functions really doing something very different ?

              Of course. Every card is unique. They share something in common but there are always differences. I may even need more than 1 functions for a card.

              1 Reply Last reply
              0
              • M MasterBlade

                @SGaist said in How to call 200 functions with 1 variable?:

                Hi,

                What are these 200 functions ?

                @jsulm said in How to call 200 functions with 1 variable?:

                @MasterBlade Why do you need 200(!) functions? Or do you mean you want to call one using an index?

                I'm designing a card game. It currently has about 200 cards. There needs to be 1 function for each of them to do something.

                I think it should be possible to call the corresponding function with only the card #

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @MasterBlade said in How to call 200 functions with 1 variable?:

                I think it should be possible to call the corresponding function with only the card #

                See the code I posted above

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @MasterBlade said in How to call 200 functions with 1 variable?:

                  I think it should be possible to call the corresponding function with only the card #

                  See the code I posted above

                  M Offline
                  M Offline
                  MasterBlade
                  wrote on last edited by
                  #8

                  @jsulm said in How to call 200 functions with 1 variable?:

                  @MasterBlade said in How to call 200 functions with 1 variable?:

                  I think it should be possible to call the corresponding function with only the card #

                  See the code I posted above

                  Yeah sure that works. Thank you so much.

                  One more question please,

                  Is it possible to store those function to an INI file on a hard disc? So that I don't need to store all 200 functions to my program. And that I can take only what I need without bothering other functions.

                  jsulmJ J.HilkJ 2 Replies Last reply
                  0
                  • M MasterBlade

                    @jsulm said in How to call 200 functions with 1 variable?:

                    @MasterBlade said in How to call 200 functions with 1 variable?:

                    I think it should be possible to call the corresponding function with only the card #

                    See the code I posted above

                    Yeah sure that works. Thank you so much.

                    One more question please,

                    Is it possible to store those function to an INI file on a hard disc? So that I don't need to store all 200 functions to my program. And that I can take only what I need without bothering other functions.

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @MasterBlade How do you want to store C++ functions in a text file?
                    And what is the problem you want to solve with this? You would still need to store 200 functions somewhere, right?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @MasterBlade How do you want to store C++ functions in a text file?
                      And what is the problem you want to solve with this? You would still need to store 200 functions somewhere, right?

                      M Offline
                      M Offline
                      MasterBlade
                      wrote on last edited by
                      #10

                      @jsulm said in How to call 200 functions with 1 variable?:

                      @MasterBlade How do you want to store C++ functions in a text file?
                      And what is the problem you want to solve with this? You would still need to store 200 functions somewhere, right?

                      Well I want to store them in a text file so that I only read what I need from it.

                      Maybe I can pick up some key elements from them and make a universal function to solve all problems once and for all.

                      jsulmJ 1 Reply Last reply
                      0
                      • M MasterBlade

                        @jsulm said in How to call 200 functions with 1 variable?:

                        @MasterBlade How do you want to store C++ functions in a text file?
                        And what is the problem you want to solve with this? You would still need to store 200 functions somewhere, right?

                        Well I want to store them in a text file so that I only read what I need from it.

                        Maybe I can pick up some key elements from them and make a universal function to solve all problems once and for all.

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @MasterBlade Yes, you should rather store the data you need for all these cards, instead of functions. Then use one function with these different data.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3
                        • M MasterBlade

                          @jsulm said in How to call 200 functions with 1 variable?:

                          @MasterBlade said in How to call 200 functions with 1 variable?:

                          I think it should be possible to call the corresponding function with only the card #

                          See the code I posted above

                          Yeah sure that works. Thank you so much.

                          One more question please,

                          Is it possible to store those function to an INI file on a hard disc? So that I don't need to store all 200 functions to my program. And that I can take only what I need without bothering other functions.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          hi @MasterBlade ,

                          I have the feeling you're approching this subject from the wrong angle.

                          What you should do is create a base class Cards that covers all functions / attributes / properties that all cards have (e.g color: ["check","cross","hearts"..], [played/not played] etc, and than derive individual classes from base class Joker, Ace, King etc.

                          You know, object oriented, the way it's thaught :-)


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          M 1 Reply Last reply
                          3
                          • J.HilkJ J.Hilk

                            hi @MasterBlade ,

                            I have the feeling you're approching this subject from the wrong angle.

                            What you should do is create a base class Cards that covers all functions / attributes / properties that all cards have (e.g color: ["check","cross","hearts"..], [played/not played] etc, and than derive individual classes from base class Joker, Ace, King etc.

                            You know, object oriented, the way it's thaught :-)

                            M Offline
                            M Offline
                            MasterBlade
                            wrote on last edited by
                            #13

                            @J.Hilk said in How to call 200 functions with 1 variable?:

                            hi @MasterBlade ,

                            I have the feeling you're approching this subject from the wrong angle.

                            What you should do is create a base class Cards that covers all functions / attributes / properties that all cards have (e.g color: ["check","cross","hearts"..], [played/not played] etc, and than derive individual classes from base class Joker, Ace, King etc.

                            You know, object oriented, the way it's thaught :-)

                            Thanks for the suggestion. You are absolutely correct. That's what I'm doing now.

                            I already have a big class containing all the basic properties of those cards. I stored these properties in a text file on my hard disk.

                            But as for those functions, I don't have an idea on how to deal with them, since every card has unique functions, (some would allow you to draw more cards, while others deal damage to other cards) and it would be difficult to store functions directly on a hard disk.

                            I am now trying to disassemble those functions to some basic elements. That's good for normalization, but it proves to be hard labor work. Maybe I should just add those functions to the program manually.

                            kshegunovK 1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #14

                              Hi
                              Its sounds like a very odd design.
                              Are you using virtual functions ?
                              Polymorphic behavior is the trademark of c++/OOP.
                              http://www.cplusplus.com/doc/tutorial/polymorphism/

                              So you base class should define the basic features of a card, and you would subclass
                              the base class to override the functions that changes what should happen pr card type.

                              M 1 Reply Last reply
                              2
                              • M MasterBlade

                                @J.Hilk said in How to call 200 functions with 1 variable?:

                                hi @MasterBlade ,

                                I have the feeling you're approching this subject from the wrong angle.

                                What you should do is create a base class Cards that covers all functions / attributes / properties that all cards have (e.g color: ["check","cross","hearts"..], [played/not played] etc, and than derive individual classes from base class Joker, Ace, King etc.

                                You know, object oriented, the way it's thaught :-)

                                Thanks for the suggestion. You are absolutely correct. That's what I'm doing now.

                                I already have a big class containing all the basic properties of those cards. I stored these properties in a text file on my hard disk.

                                But as for those functions, I don't have an idea on how to deal with them, since every card has unique functions, (some would allow you to draw more cards, while others deal damage to other cards) and it would be difficult to store functions directly on a hard disk.

                                I am now trying to disassemble those functions to some basic elements. That's good for normalization, but it proves to be hard labor work. Maybe I should just add those functions to the program manually.

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #15

                                @MasterBlade said in How to call 200 functions with 1 variable?:

                                But as for those functions, I don't have an idea on how to deal with them, since every card has unique functions, (some would allow you to draw more cards, while others deal damage to other cards) and it would be difficult to store functions directly on a hard disk.

                                Apply once more what mr. Hilk wrote up there, you need to design first and then go to implementing. Meaning that what you describe is a prime candidate for composition tied together with polymorphism. Every action would be a class, and all actions a card can do would derive from the same (abstract) base class. Then the actions would be composed into the cards, or in other words a card will keep a list of actions that it'd do when called upon. Then you can write generic code for the card that iterates all the actions from the list through a base pointer and invokes them.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                3
                                • mrjjM mrjj

                                  Hi
                                  Its sounds like a very odd design.
                                  Are you using virtual functions ?
                                  Polymorphic behavior is the trademark of c++/OOP.
                                  http://www.cplusplus.com/doc/tutorial/polymorphism/

                                  So you base class should define the basic features of a card, and you would subclass
                                  the base class to override the functions that changes what should happen pr card type.

                                  M Offline
                                  M Offline
                                  MasterBlade
                                  wrote on last edited by
                                  #16

                                  @mrjj said in How to call 200 functions with 1 variable?:

                                  Hi
                                  Its sounds like a very odd design.
                                  Are you using virtual functions ?
                                  Polymorphic behavior is the trademark of c++/OOP.
                                  http://www.cplusplus.com/doc/tutorial/polymorphism/

                                  So you base class should define the basic features of a card, and you would subclass
                                  the base class to override the functions that changes what should happen pr card type.

                                  Thank you so much for the advice. I do appreciate it.

                                  My program is very basic. I used one object for the cards and another for players.

                                  All cards share something in common, like their properties or attributes which can be stored as variables. But their abilities are unique for each card and I am not sure if I can store them with variables. If I can't I have to use functions for each of them.

                                  For now I'm storing all card info (with variables) in a text file. And I only initialize cards necessary for the game (usually 30-40 unique cards in a 'Deck'). If I have to use functions, since I can't store functions in a text file I have to initialize all cards in my program which cost memory and time. I will expand the card pool later and likely to have a 3,000 card pool in total. That would be insane.

                                  JonBJ 1 Reply Last reply
                                  0
                                  • M MasterBlade

                                    @mrjj said in How to call 200 functions with 1 variable?:

                                    Hi
                                    Its sounds like a very odd design.
                                    Are you using virtual functions ?
                                    Polymorphic behavior is the trademark of c++/OOP.
                                    http://www.cplusplus.com/doc/tutorial/polymorphism/

                                    So you base class should define the basic features of a card, and you would subclass
                                    the base class to override the functions that changes what should happen pr card type.

                                    Thank you so much for the advice. I do appreciate it.

                                    My program is very basic. I used one object for the cards and another for players.

                                    All cards share something in common, like their properties or attributes which can be stored as variables. But their abilities are unique for each card and I am not sure if I can store them with variables. If I can't I have to use functions for each of them.

                                    For now I'm storing all card info (with variables) in a text file. And I only initialize cards necessary for the game (usually 30-40 unique cards in a 'Deck'). If I have to use functions, since I can't store functions in a text file I have to initialize all cards in my program which cost memory and time. I will expand the card pool later and likely to have a 3,000 card pool in total. That would be insane.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #17

                                    @MasterBlade

                                    • It would be insane to have 3,000 different functions/sub-classes!
                                    • You cannot store C++ source code in a file to be "read in and acted upon" at runtime.
                                    • You want to create whatever common code for all cards in your Card class in C++ (compiled) code.
                                    • You want to store whatever information about each individual card as data (not code) --- a description of its attributes, effects, its image, etc. --- in some kind of external file. It could be XML, JSON or whatever, or it could even be stored in a database.
                                    • You want to read/access that at runtime as required, and your C++ code should be generic enough that it can look at that data and have code which acts upon it to produce the desired effects.
                                    M 1 Reply Last reply
                                    3
                                    • JonBJ JonB

                                      @MasterBlade

                                      • It would be insane to have 3,000 different functions/sub-classes!
                                      • You cannot store C++ source code in a file to be "read in and acted upon" at runtime.
                                      • You want to create whatever common code for all cards in your Card class in C++ (compiled) code.
                                      • You want to store whatever information about each individual card as data (not code) --- a description of its attributes, effects, its image, etc. --- in some kind of external file. It could be XML, JSON or whatever, or it could even be stored in a database.
                                      • You want to read/access that at runtime as required, and your C++ code should be generic enough that it can look at that data and have code which acts upon it to produce the desired effects.
                                      M Offline
                                      M Offline
                                      MasterBlade
                                      wrote on last edited by
                                      #18

                                      @JonB said in How to call 200 functions with 1 variable?:

                                      @MasterBlade

                                      • It would be insane to have 3,000 different functions/sub-classes!
                                      • You cannot store C++ source code in a file to be "read in and acted upon" at runtime.
                                      • You want to create whatever common code for all cards in your Card class in C++ (compiled) code.
                                      • You want to store whatever information about each individual card as data (not code) --- a description of its attributes, effects, its image, etc. --- in some kind of external file. It could be XML, JSON or whatever, or it could even be stored in a database.
                                      • You want to read/access that at runtime as required, and your C++ code should be generic enough that it can look at that data and have code which acts upon it to produce the desired effects.

                                      Thanks for the suggestion. Sorry I didn't read the post yesterday.

                                      I totally agree that 3,000 functions are insane. But 200 is rather acceptable. I want to make life easier by writing 200 functions for now and make a running program. I'll disassemble those functions and pick out those basic elements to make a universal function that covers all card functionalities for future implements.

                                      mrjjM 1 Reply Last reply
                                      0
                                      • M MasterBlade

                                        @JonB said in How to call 200 functions with 1 variable?:

                                        @MasterBlade

                                        • It would be insane to have 3,000 different functions/sub-classes!
                                        • You cannot store C++ source code in a file to be "read in and acted upon" at runtime.
                                        • You want to create whatever common code for all cards in your Card class in C++ (compiled) code.
                                        • You want to store whatever information about each individual card as data (not code) --- a description of its attributes, effects, its image, etc. --- in some kind of external file. It could be XML, JSON or whatever, or it could even be stored in a database.
                                        • You want to read/access that at runtime as required, and your C++ code should be generic enough that it can look at that data and have code which acts upon it to produce the desired effects.

                                        Thanks for the suggestion. Sorry I didn't read the post yesterday.

                                        I totally agree that 3,000 functions are insane. But 200 is rather acceptable. I want to make life easier by writing 200 functions for now and make a running program. I'll disassemble those functions and pick out those basic elements to make a universal function that covers all card functionalities for future implements.

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @MasterBlade
                                        So its really 200 different function with nothing in common ?
                                        like
                                        Card 1
                                        Burn()
                                        Card 2
                                        Freeze()
                                        Card 3
                                        ExtraTurn()
                                        ....

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved