Thread View: comp.sys.mac.programmer.codewarrior
3 messages
3 total messages
Started by R Shapiro
Thu, 01 Feb 1996 13:33
templatized pointers to member functions (in CW8)
Author: R Shapiro
Date: Thu, 01 Feb 1996 13:33
Date: Thu, 01 Feb 1996 13:33
48 lines
1653 bytes
1653 bytes
I need to reference a pointer to a member function within a template,
where the class of the function is determined by the template parameter.
Consider:
#include <iostream.h>
class ATest {
typedef void (ATest::*ATestObjectMemberFunction)(void);
public:
ATest() {}
void junk () { cout << "Junk" << endl << flush;}
ATestObjectMemberFunction func () { return &ATest::junk; }
};
template <class OBJECT>
class Test {
// (a) This is what I want but it won't compile in CodeWarrior 8:
//typedef void (OBJECT::*ObjectMemberFunction)(void);
// (b) This works but isn't what I want:
typedef void (ATest::*ObjectMemberFunction)(void);
private:
OBJECT& object;
public:
Test(OBJECT& object) : object(object) {}
void run(ObjectMemberFunction func) { (object.*func)(); }
};
int main ()
{
ATest aTest;
Test<ATest> aTestTemp (aTest);
aTestTemp.run(aTest.func());
return 0;
}
My first question is, should (a) work? A reading of Stroustrup doesn't
make this clear, at least not to me. Second question: if it shouldn't, is
there another way to do what I'm after here? Third question, for
Metrowerks: if it should work, do you plan to support it soon?
--
rs/rshapiro@bbn.com
Re: templatized pointers to member functions (in CW8)
Author: G. Patrick Sand
Date: Thu, 01 Feb 1996 18:55
Date: Thu, 01 Feb 1996 18:55
47 lines
2025 bytes
2025 bytes
In article <rshapiro-0102960833080001@ipa.bbn.com>, rshapiro@bbn.com
says...
>
>I need to reference a pointer to a member function within a template,
>where the class of the function is determined by the template parameter.
>
>class ATest {
[snip!]
>};
>
>template <class OBJECT>
>class Test {
> // (a) This is what I want but it won't compile in CodeWarrior 8:
> //typedef void (OBJECT::*ObjectMemberFunction)(void);
>
> // (b) This works but isn't what I want:
> typedef void (ATest::*ObjectMemberFunction)(void);
>
[snip!]
>};
I think the answer is NO. A template is a cookie cutter for whatever you
specify, but it doesn't assume any knowledge about what you specify
beyond whether it is a class template or a function template. In this
case, knowing that a particular function is in the class (your desired
typedef with the OBJECT::*ObjectMemberFunction... specification) is what
is causing the problem.
When you specify a class the compiler knows about, namely ATest (i.e.,
ATest::*ObjectMemberFunction...) it is happy to oblige...
I dug this out of the C++ FAQ book, although it wasn't explicitly
discussed.
So I might be wrong...
--
G. Patrick Sand
psand@eos.hitc.com
PatSand@aol.com
(301) 925-0791
"Travel Light But Right..."
Microsoft Network is prohibited from redistributing
this work in any form, in whole or in part. License
to distribute this individual post is available to Microsoft
for $999. Posting without permission constitutes an
agreement to these terms...gps
Re: templatized pointers to member functions (in CW8)
Author: R Shapiro
Date: Thu, 01 Feb 1996 20:30
Date: Thu, 01 Feb 1996 20:30
31 lines
1343 bytes
1343 bytes
In article <4er2af$nna@newsroom.hitc.com>, psand@eos.hitc.com (G. Patrick
Sand) wrote:
>>template <class OBJECT>
>>class Test {
>> // (a) This is what I want but it won't compile in CodeWarrior 8:
>> //typedef void (OBJECT::*ObjectMemberFunction)(void);
>>
>> // (b) This works but isn't what I want:
>> typedef void (ATest::*ObjectMemberFunction)(void);
>>
>[snip!]
>>};
>
>I think the answer is NO. A template is a cookie cutter for whatever you
>specify, but it doesn't assume any knowledge about what you specify
>beyond whether it is a class template or a function template.
I'm not asking it to. I just want it to substitute the real class name for
OBJECT in:
typedef void (OBJECT::*ObjectMemberFunction)(void);
just as it does with all other references to OBJECT within the template.
VisualC++ 4.0 does this successfully, by the way.
--
rs/rshapiro@bbn.com
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads