Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Function pointer problem

Function pointer problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 5 Posters 2 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jpg 0
    wrote on last edited by
    #1

    #include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }

    D S P 3 Replies Last reply
    0
    • J jpg 0

      #include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      What exactly is the problem?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

      A 1 Reply Last reply
      0
      • J jpg 0

        #include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }

        S Offline
        S Offline
        smags13
        wrote on last edited by
        #3

        try using static inside the class. http://www.parashift.com/c++-faq-lite/pointers-to-members.html[^]

        A 1 Reply Last reply
        0
        • D David Crow

          What exactly is the problem?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

          A Offline
          A Offline
          Albert Holguin
          wrote on last edited by
          #4

          he did label the problem, look for the comments ('//')

          D 1 Reply Last reply
          0
          • A Albert Holguin

            he did label the problem, look for the comments ('//')

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            "but this does not work!?" is not a useful problem description. It could be a compiler or linker problem. It could be an assertion. It could be an exception. It could be a logic problem.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

            A 1 Reply Last reply
            0
            • D David Crow

              "but this does not work!?" is not a useful problem description. It could be a compiler or linker problem. It could be an assertion. It could be an exception. It could be a logic problem.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

              A Offline
              A Offline
              Albert Holguin
              wrote on last edited by
              #6

              It is if you look at the difference between his two statements

              1 Reply Last reply
              0
              • S smags13

                try using static inside the class. http://www.parashift.com/c++-faq-lite/pointers-to-members.html[^]

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                good link! ...you may be right, I didn't think about the address being different for non-statics.

                1 Reply Last reply
                0
                • J jpg 0

                  #include #include using namespace std; void Print(const string& value) { cout << value << endl; } void PrintBy (const string& value, void (*printer)(const string& value)) { printer(value); } class Printer { public: void Print(const string& value) { cout << value << endl; } }; int main() { string s = "hi"; Print(s); // why this work? PrintBy(s, &Print); // but this does not work!? Printer p; PrintBy(s, &p.Print); return 0; }

                  P Offline
                  P Offline
                  Paul Michalik
                  wrote on last edited by
                  #8

                  What do you mean by "it does not work"?... The code as written just does not compile for a simple reason: Your second call to PrintBy does not match the signature of the called function. You either declare a second overload for PrintBy taking a proper pointer-to-method of Printer or (much better) you rely on standard library abilities:

                  #include #include template < typename TPrinter >
                  void PrintBy( std::string const & pString, TPrinter pPrinter )
                  {
                  pPrinter(pString);
                  }

                  // Your stuff ...

                  int main()
                  {
                  ::PrintBy( "hi", Print );

                  ::Printer p;
                  
                  ::PrintBy( "hi again", std::bind( &::Printer::Print, &p, std::placeholders::\_1 ) );
                  

                  }

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups