This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: bug in release-1.0 with respect to method pointers?


> 
> 
> Hello.  I'm trying to compile a large project I'm working on with
> egcs-1.0.  I'm immediatley having compilation problems.  Here is some code
> that precisely illustrates the problem:
> 
> [ begin included source ]
> 
> #include <iostream.h>
> 
> class abstractBase {
> public:
>   abstractBase(){}
>   ~abstractBase(){}
>   
>   virtual int returnInt(){
>     return -1;
>   }
> };
> 
> class commonAbstract : abstractBase {
> public:
>   commonAbstract(){}
>   ~commonAbstract(){}
>   
>   virtual int returnInt(){
>     return -1;
>   }
> };
> 
> class derived1 : public commonAbstract {
> public:
>   derived1(){}
>   ~derived1(){}
> 
>   int returnInt(){
>     return 1;
>   }
> };
> 
> class derived2 : public commonAbstract {
> public:
>   derived2(){}
>   ~derived2(){}
>   
> };
> 
> class unrelated {
> public:
>   unrelated(){}
>   ~unrelated(){}
>   
>   void printResult(commonAbstract *callOn, int(commonAbstract::*memberFunction)() ){
>     cout << (callOn->*memberFunction)() << endl;
>   }
>   
> };
> 
> int 
> main( int argc, char *argv[] ){
>   commonAbstract *ca1, *ca2;
>   unrelated *u;
> 
>   u = new unrelated();
>   ca1 = new derived1();
>   ca2 = new derived2();
> 
>   cout << "Testing on a derived1" << endl;
>   u->printResult( ca1, commonAbstract::returnInt );
> 
>   cout << "Testing on a derived2" << endl;
>   u->printResult( ca2, commonAbstract::returnInt );
> }
> 

I think there is a difference in g++ between the address of a
member function and a pointer to a member function. In g++,
"commonAbstract::returnInt" gives the memory address of
commonAbstract::returnInt and "&commonAbstract::returnInt"
gives a pointer to commonAbstract::returnInt.

Not all C++ compilers do that. VC++ 5.0 treats them the same.
Personally I like the g++ way. I have some code depending on it.
I know it is very compiler specific. But it seems to be a simple
way to pass a non-virtual member function and THIS to a thread
creatation function. Without it, I have to write more code
for it.


H.J.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]