Function, that creates other functions c++ [closed]












-4















I have a task to create function Factory(const std::string name) that returns pointer to function without arguments that prints name. Also I should use only native language methods (without lambda functions and etc). Could you give me example?










share|improve this question















closed as off-topic by Nicky C, llllllllll, hellow, JBL, Rob Nov 23 '18 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – llllllllll, JBL, Rob

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1





    "native language methods"? What do you even mean? Also, asking for off-site resources is off-topic.

    – Nicky C
    Nov 23 '18 at 12:54






  • 3





    Well lambdas are what I would call a "native language method", as it's an integral part of the C++ language.

    – Some programmer dude
    Nov 23 '18 at 12:55











  • As for this specific case it's very easy to create a class with a function-call operator that does exactly what you want, and return an instance of that (with the right constructor it will make your Factor function into a single-line function, containing only a suitable return statement).

    – Some programmer dude
    Nov 23 '18 at 12:56








  • 2





    Lastly, please read about how to ask good questions, as well as this question checklist. And learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 12:58






  • 1





    @Blood-HaZaRd Point taken :-) I should probably have left more things unresolved.

    – Ted Lyngmo
    Nov 23 '18 at 13:25
















-4















I have a task to create function Factory(const std::string name) that returns pointer to function without arguments that prints name. Also I should use only native language methods (without lambda functions and etc). Could you give me example?










share|improve this question















closed as off-topic by Nicky C, llllllllll, hellow, JBL, Rob Nov 23 '18 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – llllllllll, JBL, Rob

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1





    "native language methods"? What do you even mean? Also, asking for off-site resources is off-topic.

    – Nicky C
    Nov 23 '18 at 12:54






  • 3





    Well lambdas are what I would call a "native language method", as it's an integral part of the C++ language.

    – Some programmer dude
    Nov 23 '18 at 12:55











  • As for this specific case it's very easy to create a class with a function-call operator that does exactly what you want, and return an instance of that (with the right constructor it will make your Factor function into a single-line function, containing only a suitable return statement).

    – Some programmer dude
    Nov 23 '18 at 12:56








  • 2





    Lastly, please read about how to ask good questions, as well as this question checklist. And learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 12:58






  • 1





    @Blood-HaZaRd Point taken :-) I should probably have left more things unresolved.

    – Ted Lyngmo
    Nov 23 '18 at 13:25














-4












-4








-4








I have a task to create function Factory(const std::string name) that returns pointer to function without arguments that prints name. Also I should use only native language methods (without lambda functions and etc). Could you give me example?










share|improve this question
















I have a task to create function Factory(const std::string name) that returns pointer to function without arguments that prints name. Also I should use only native language methods (without lambda functions and etc). Could you give me example?







c++ task function-pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:57







Jekyll Hyde

















asked Nov 23 '18 at 12:53









Jekyll HydeJekyll Hyde

12




12




closed as off-topic by Nicky C, llllllllll, hellow, JBL, Rob Nov 23 '18 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – llllllllll, JBL, Rob

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Nicky C, llllllllll, hellow, JBL, Rob Nov 23 '18 at 14:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – llllllllll, JBL, Rob

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1





    "native language methods"? What do you even mean? Also, asking for off-site resources is off-topic.

    – Nicky C
    Nov 23 '18 at 12:54






  • 3





    Well lambdas are what I would call a "native language method", as it's an integral part of the C++ language.

    – Some programmer dude
    Nov 23 '18 at 12:55











  • As for this specific case it's very easy to create a class with a function-call operator that does exactly what you want, and return an instance of that (with the right constructor it will make your Factor function into a single-line function, containing only a suitable return statement).

    – Some programmer dude
    Nov 23 '18 at 12:56








  • 2





    Lastly, please read about how to ask good questions, as well as this question checklist. And learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 12:58






  • 1





    @Blood-HaZaRd Point taken :-) I should probably have left more things unresolved.

    – Ted Lyngmo
    Nov 23 '18 at 13:25














  • 1





    "native language methods"? What do you even mean? Also, asking for off-site resources is off-topic.

    – Nicky C
    Nov 23 '18 at 12:54






  • 3





    Well lambdas are what I would call a "native language method", as it's an integral part of the C++ language.

    – Some programmer dude
    Nov 23 '18 at 12:55











  • As for this specific case it's very easy to create a class with a function-call operator that does exactly what you want, and return an instance of that (with the right constructor it will make your Factor function into a single-line function, containing only a suitable return statement).

    – Some programmer dude
    Nov 23 '18 at 12:56








  • 2





    Lastly, please read about how to ask good questions, as well as this question checklist. And learn how to create a Minimal, Complete, and Verifiable example.

    – Some programmer dude
    Nov 23 '18 at 12:58






  • 1





    @Blood-HaZaRd Point taken :-) I should probably have left more things unresolved.

    – Ted Lyngmo
    Nov 23 '18 at 13:25








1




1





"native language methods"? What do you even mean? Also, asking for off-site resources is off-topic.

– Nicky C
Nov 23 '18 at 12:54





"native language methods"? What do you even mean? Also, asking for off-site resources is off-topic.

– Nicky C
Nov 23 '18 at 12:54




3




3





Well lambdas are what I would call a "native language method", as it's an integral part of the C++ language.

– Some programmer dude
Nov 23 '18 at 12:55





Well lambdas are what I would call a "native language method", as it's an integral part of the C++ language.

– Some programmer dude
Nov 23 '18 at 12:55













As for this specific case it's very easy to create a class with a function-call operator that does exactly what you want, and return an instance of that (with the right constructor it will make your Factor function into a single-line function, containing only a suitable return statement).

– Some programmer dude
Nov 23 '18 at 12:56







As for this specific case it's very easy to create a class with a function-call operator that does exactly what you want, and return an instance of that (with the right constructor it will make your Factor function into a single-line function, containing only a suitable return statement).

– Some programmer dude
Nov 23 '18 at 12:56






2




2





Lastly, please read about how to ask good questions, as well as this question checklist. And learn how to create a Minimal, Complete, and Verifiable example.

– Some programmer dude
Nov 23 '18 at 12:58





Lastly, please read about how to ask good questions, as well as this question checklist. And learn how to create a Minimal, Complete, and Verifiable example.

– Some programmer dude
Nov 23 '18 at 12:58




1




1





@Blood-HaZaRd Point taken :-) I should probably have left more things unresolved.

– Ted Lyngmo
Nov 23 '18 at 13:25





@Blood-HaZaRd Point taken :-) I should probably have left more things unresolved.

– Ted Lyngmo
Nov 23 '18 at 13:25












2 Answers
2






active

oldest

votes


















1














Annotations in the code. I deliberately did not make it print to std::cout directly but instead it will return the string. Adapt it as you please.



#include <iostream>

struct bork { // the object to hold the text to return
std::string text; // the text to return

// constructor
bork(const std::string& in) : text(in) {}

// the operator that makes the object behave like a function
std::string operator ()(void) const { return text; }

// a factory method to create a "bork"
static bork make_bork(const std::string& text) {
return bork(text);
}

};

int main() {
auto a = bork::make_bork("howdy");
auto b = bork::make_bork("world");

std::cout << a() << "n";
std::cout << b() << "n";
}





share|improve this answer































    -1














    You cannot create a function in a function. The only way is to know all the strings there will appear and having a functions for each possible string, and then select and return the proper function. Or else you can use objects :-)






    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Annotations in the code. I deliberately did not make it print to std::cout directly but instead it will return the string. Adapt it as you please.



      #include <iostream>

      struct bork { // the object to hold the text to return
      std::string text; // the text to return

      // constructor
      bork(const std::string& in) : text(in) {}

      // the operator that makes the object behave like a function
      std::string operator ()(void) const { return text; }

      // a factory method to create a "bork"
      static bork make_bork(const std::string& text) {
      return bork(text);
      }

      };

      int main() {
      auto a = bork::make_bork("howdy");
      auto b = bork::make_bork("world");

      std::cout << a() << "n";
      std::cout << b() << "n";
      }





      share|improve this answer




























        1














        Annotations in the code. I deliberately did not make it print to std::cout directly but instead it will return the string. Adapt it as you please.



        #include <iostream>

        struct bork { // the object to hold the text to return
        std::string text; // the text to return

        // constructor
        bork(const std::string& in) : text(in) {}

        // the operator that makes the object behave like a function
        std::string operator ()(void) const { return text; }

        // a factory method to create a "bork"
        static bork make_bork(const std::string& text) {
        return bork(text);
        }

        };

        int main() {
        auto a = bork::make_bork("howdy");
        auto b = bork::make_bork("world");

        std::cout << a() << "n";
        std::cout << b() << "n";
        }





        share|improve this answer


























          1












          1








          1







          Annotations in the code. I deliberately did not make it print to std::cout directly but instead it will return the string. Adapt it as you please.



          #include <iostream>

          struct bork { // the object to hold the text to return
          std::string text; // the text to return

          // constructor
          bork(const std::string& in) : text(in) {}

          // the operator that makes the object behave like a function
          std::string operator ()(void) const { return text; }

          // a factory method to create a "bork"
          static bork make_bork(const std::string& text) {
          return bork(text);
          }

          };

          int main() {
          auto a = bork::make_bork("howdy");
          auto b = bork::make_bork("world");

          std::cout << a() << "n";
          std::cout << b() << "n";
          }





          share|improve this answer













          Annotations in the code. I deliberately did not make it print to std::cout directly but instead it will return the string. Adapt it as you please.



          #include <iostream>

          struct bork { // the object to hold the text to return
          std::string text; // the text to return

          // constructor
          bork(const std::string& in) : text(in) {}

          // the operator that makes the object behave like a function
          std::string operator ()(void) const { return text; }

          // a factory method to create a "bork"
          static bork make_bork(const std::string& text) {
          return bork(text);
          }

          };

          int main() {
          auto a = bork::make_bork("howdy");
          auto b = bork::make_bork("world");

          std::cout << a() << "n";
          std::cout << b() << "n";
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 13:12









          Ted LyngmoTed Lyngmo

          2,1151317




          2,1151317

























              -1














              You cannot create a function in a function. The only way is to know all the strings there will appear and having a functions for each possible string, and then select and return the proper function. Or else you can use objects :-)






              share|improve this answer




























                -1














                You cannot create a function in a function. The only way is to know all the strings there will appear and having a functions for each possible string, and then select and return the proper function. Or else you can use objects :-)






                share|improve this answer


























                  -1












                  -1








                  -1







                  You cannot create a function in a function. The only way is to know all the strings there will appear and having a functions for each possible string, and then select and return the proper function. Or else you can use objects :-)






                  share|improve this answer













                  You cannot create a function in a function. The only way is to know all the strings there will appear and having a functions for each possible string, and then select and return the proper function. Or else you can use objects :-)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 13:29









                  guestguest

                  542




                  542















                      Popular posts from this blog

                      Berounka

                      Sphinx de Gizeh

                      Fiat S.p.A.