Hi all, I got a code in the net. I executed that one , it is working fine..I did not understand how it is working.. can anybody explain me? code is.. c12.h Code: #ifndef C12_H #define C12_H #include <iostream.h> class C1 { public: void f1( int arg = 0 ) {cout<<"inside f1"<<endl;} // }; class C2 { public: void f2() {cout<<"inside f2"<<endl;} void f2( int) {cout<<"inside f2(int)"<<endl;} // ... }; #endif c12.cpp file Code: // Failure to Distinguish Overloading from Default Initialization #include "c12.h" int main() { C1 c1; c1.f1(0); c1.f1(); C2 c2; c2.f2(0); c2.f2(); //void (C1::*pmf1)() = &C1::f1; //error! void (C2::*pmf2)() = &C2::f2; return 0; } Regards, sharmila.
please use the proper bbcode so that code is well formatted. Also regarding your post what is the thing that made you think like that. I dont think there is anything which should not allow the above code to work.
Hi Shabbir, My doubt is if we uncomment void (C1::*pmf1)() = &C1::f1; it showing error?But the samething is working fine for C2..the only difference is f1() is taking default argument.The error it is showing is error C2440: 'initializing' : cannot convert from 'void (__thiscall C1::*)(int)' to 'void (__thiscall C1::*)(void)' Here it is not recognizing default argument..why it is so.. Thanks and regards, sharmila.
Actually void (C1::*pmf1)() means it can hold the pointer to a function having no parameter but f1 in C1 has an argument. The argument is such that if you dont provide a value it can have a default value. Its confusing with the constructor where having a default constructor is not needed if you specify the values of each of the parameters in the non -default constructor making it default.
Hi shabbir, It will happen in overloading.But for class C1 we did not write any constructor, so it will invoke default one.Here it is able to create C1 object but unable to assign function for function pointer. Regards, sharmila.
No probably I could not make you clearly understand what I meant. When we write any constructor having all the parameters default value we call him default constructor but actually the default constructor with no param is different in function signature than having default params of all. I hope now its a bit more clearer