Why doesn't this work?
-
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
-
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
myhanguk wrote:
, but crashes.
It is because, you are using
nrand
without initiazing it. Ask such questions in VC++ forum.
Prasad MS MVP - VC++
-
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
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
-
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
myhanguk wrote:
double rand_mt(void){ blockquote> i am beginer in c++ but i want to know what (void) refere to? thanks
****************** ** Ahmed Ismail ** ******************