* bug in release-1.0 with respect to method pointers?
@ 1997-12-04 13:27 Dale Martin
1997-12-04 15:02 ` Olivier Galibert
1997-12-04 17:14 ` H.J. Lu
0 siblings, 2 replies; 3+ messages in thread
From: Dale Martin @ 1997-12-04 13:27 UTC (permalink / raw)
To: egcs
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 );
}
[ end included source ]
This code compiles and behaves as expected with g++-2.7.2.3 and SunPro
4.2.
(Here is the output:)
Testing on a derived1
1
Testing on a derived2
-1
With egcs-1.0 I get:
bug.cc: In function `int main(int, char **)':
bug.cc:61: no matching function for call to `unrelated::printResult (commonAbstract *&, int (commonAbstract::)())'
bug.cc:45: candidates are: unrelated::printResult(commonAbstract *, int (commonAbstract::*)())
bug.cc:64: no matching function for call to `unrelated::printResult (commonAbstract *&, int (commonAbstract::)())'
bug.cc:45: candidates are: unrelated::printResult(commonAbstract *, int (commonAbstract::*)())
I'm compiling on a linux/libc6 system. I enable haifa extensions when I
configured egcs.
I realize this could be a problem in my code - it's kind of a weird thing
to do :-)
Thanks,
Dale
--
+-------------------- finger for pgp public key ---------------------+
| Dale E. Martin | University of Cincinnati Savant Research Laboratory |
| dmartin@ececs.uc.edu | http://www.ececs.uc.edu/~dmartin |
+----------------------------------------------------------------------+
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: bug in release-1.0 with respect to method pointers?
1997-12-04 13:27 bug in release-1.0 with respect to method pointers? Dale Martin
@ 1997-12-04 15:02 ` Olivier Galibert
1997-12-04 17:14 ` H.J. Lu
1 sibling, 0 replies; 3+ messages in thread
From: Olivier Galibert @ 1997-12-04 15:02 UTC (permalink / raw)
To: Dale Martin; +Cc: egcs
On Thu, Dec 04, 1997 at 11:18:02AM -0500, Dale Martin wrote:
> 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:
Your code is invalid. Use (note the &) :
u->printResult( ca1, &commonAbstract::returnInt );
and then it works.
OG.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: bug in release-1.0 with respect to method pointers?
1997-12-04 13:27 bug in release-1.0 with respect to method pointers? Dale Martin
1997-12-04 15:02 ` Olivier Galibert
@ 1997-12-04 17:14 ` H.J. Lu
1 sibling, 0 replies; 3+ messages in thread
From: H.J. Lu @ 1997-12-04 17:14 UTC (permalink / raw)
To: Dale Martin; +Cc: egcs
>
>
> 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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1997-12-04 17:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-04 13:27 bug in release-1.0 with respect to method pointers? Dale Martin
1997-12-04 15:02 ` Olivier Galibert
1997-12-04 17:14 ` H.J. Lu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).