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. Managed C++/CLI
  4. Why doesn't this work?

Why doesn't this work?

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
4 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    myhanguk
    wrote on last edited by
    #1

    Hi... I'm having stupid problem with pointer to member functions. The following simple test program compiles, but crashes at run-time. Can anybody help me out here? class cTest{ public: double rand_mt(void){ return (1000.0); } double rand_cpp(void){ return (2000.0); } double (*cTest::nrand)(void); void init(int choice){ if (choice==1){ double (cTest::*nrand)() = &cTest::rand_cpp; } else { double (cTest::*nrand)() = &cTest::rand_mt; } } }; void main(){ cTest a; a.init(1); cout << a.nrand() << endl; } This simple program compiles, but crashes. It works fine if I don't use a class. Why? Thanks for your help, Niko

    P M A 3 Replies Last reply
    0
    • M myhanguk

      Hi... I'm having stupid problem with pointer to member functions. The following simple test program compiles, but crashes at run-time. Can anybody help me out here? class cTest{ public: double rand_mt(void){ return (1000.0); } double rand_cpp(void){ return (2000.0); } double (*cTest::nrand)(void); void init(int choice){ if (choice==1){ double (cTest::*nrand)() = &cTest::rand_cpp; } else { double (cTest::*nrand)() = &cTest::rand_mt; } } }; void main(){ cTest a; a.init(1); cout << a.nrand() << endl; } This simple program compiles, but crashes. It works fine if I don't use a class. Why? Thanks for your help, Niko

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      myhanguk wrote:

      , but crashes.

      It is because, you are using nrand without initiazing it. Ask such questions in VC++ forum.


      Prasad MS MVP -  VC++

      1 Reply Last reply
      0
      • M myhanguk

        Hi... I'm having stupid problem with pointer to member functions. The following simple test program compiles, but crashes at run-time. Can anybody help me out here? class cTest{ public: double rand_mt(void){ return (1000.0); } double rand_cpp(void){ return (2000.0); } double (*cTest::nrand)(void); void init(int choice){ if (choice==1){ double (cTest::*nrand)() = &cTest::rand_cpp; } else { double (cTest::*nrand)() = &cTest::rand_mt; } } }; void main(){ cTest a; a.init(1); cout << a.nrand() << endl; } This simple program compiles, but crashes. It works fine if I don't use a class. Why? Thanks for your help, Niko

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Try this (modifications marked in red): double (cTest::*nrand)(void); void init(int choice){ if (choice==1){ nrand = &cTest::rand_cpp; } else { nrand = &cTest::rand_mt; } } You were just assigning function pointers to local variables in init() instead of assigning to the member variable nrand. Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        1 Reply Last reply
        0
        • M myhanguk

          Hi... I'm having stupid problem with pointer to member functions. The following simple test program compiles, but crashes at run-time. Can anybody help me out here? class cTest{ public: double rand_mt(void){ return (1000.0); } double rand_cpp(void){ return (2000.0); } double (*cTest::nrand)(void); void init(int choice){ if (choice==1){ double (cTest::*nrand)() = &cTest::rand_cpp; } else { double (cTest::*nrand)() = &cTest::rand_mt; } } }; void main(){ cTest a; a.init(1); cout << a.nrand() << endl; } This simple program compiles, but crashes. It works fine if I don't use a class. Why? Thanks for your help, Niko

          A Offline
          A Offline
          Ahmed Ismail Mohamed
          wrote on last edited by
          #4

          myhanguk wrote:

          double rand_mt(void){ blockquote> i am beginer in c++ but i want to know what (void) refere to? thanks

          ****************** ** Ahmed Ismail ** ******************

          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