![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Templates: Two methods with same name
Hi,
after here I have a small EXAMPLE CODE for using functors. One problem I have is the the method "test2" in the class X and the one line with the assertion. Why does it fail when the second "test2" methods exists? How can I handle this well? thanks Thomas <code> #include <iostream> #include <assert.h> template<class TReturn, class TInstance, class TMethod> struct Functor { Functor(TInstance instance, TMethod method) : _instance(instance), _method(method) {} TReturn execute() const { return (_instance->*_method)(); } TInstance _instance; TMethod _method; }; template<class TInstance, class TMethod> struct Functor<void, TInstance, TMethod> { Functor(TInstance instance, TMethod method) : _instance(instance), _method(method) {} void execute() const { (_instance->*_method)(); } TInstance _instance; TMethod _method; }; template <class TInstance, class TMethod> Functor<void, TInstance, TMethod> makeFunctor(TInstance instance, TMethod method) { return Functor<void, TInstance, TMethod>(instance, method); } template <class TReturn, class TInstance, class TMethod> Functor<TReturn, TInstance, TMethod> makeFunctor(TInstance instance, TMethod method) { return Functor<TReturn, TInstance, TMethod>(instance, method); } class X { public: void test1() { std::cout << "X::test1()" << std::endl; } void test1const() const { std::cout << "X::test1const()" << std::endl; } int test2() { return 1024; } //int test2() const { return 2048; } }; void functor_test1(X& myx) { makeFunctor(&myx, &X::test1).execute(); assert(makeFunctor<int>(&myx, &X::test2).execute() == 1024); } void functor_test2(const X& myx) { makeFunctor(&myx, &X::test1const).execute(); //assert(makeFunctor<int>(&myx, &X::test2).execute() == 2048); } int main() { X myx; functor_test1(myx); functor_test2(myx); return 0; } </code> -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Templates: Two methods with same name
On 17 Aug., 20:52, Iris-und-Thomas-Lehm...@t-online.de wrote:
> Hi, > > after here I have a small EXAMPLE CODE for > using functors. One problem I have is the > the method "test2" in the class X and the > one line with the assertion. > > Why does it fail when the second "test2" methods exists? > How can I handle this well? > > thanks > Thomas > > <code> > #include <iostream> > #include <assert.h> > > template<class TReturn, class TInstance, class TMethod> > struct Functor > { > Functor(TInstance instance, TMethod method) > : _instance(instance), _method(method) {} > > TReturn execute() const > { return (_instance->*_method)(); } > > TInstance _instance; > TMethod _method; > > }; > > template<class TInstance, class TMethod> > struct Functor<void, TInstance, TMethod> > { > Functor(TInstance instance, TMethod method) > : _instance(instance), _method(method) {} > > void execute() const > { (_instance->*_method)(); } > > TInstance _instance; > TMethod _method; > > }; > > template <class TInstance, class TMethod> > Functor<void, TInstance, TMethod> makeFunctor(TInstance instance, > TMethod method) > { return Functor<void, TInstance, TMethod>(instance, method); } > > template <class TReturn, class TInstance, class TMethod> > Functor<TReturn, TInstance, TMethod> makeFunctor(TInstance instance, > TMethod method) > { return Functor<TReturn, TInstance, TMethod>(instance, method); } > > class X > { > public: > void test1() { std::cout << "X::test1()" << std::endl; } > void test1const() const { std::cout << "X::test1const()" << > std::endl; } > > int test2() { return 1024; } > //int test2() const { return 2048; } > > }; > > void functor_test1(X& myx) > { > makeFunctor(&myx, &X::test1).execute(); > assert(makeFunctor<int>(&myx, &X::test2).execute() == 1024); > > } > > void functor_test2(const X& myx) > { > makeFunctor(&myx, &X::test1const).execute(); > //assert(makeFunctor<int>(&myx, &X::test2).execute() == 2048); > > } > > int main() > { > X myx; > > functor_test1(myx); > functor_test2(myx); > return 0;} > > </code> { Edits: quoted banner removed. The banner is automatically appended to every article, including this one. So there's no need to quote it. -mod } Also nobody seems to be interested for help I found the solution with the help of a colleague. So this message does not stay uncompleted, here the solution: <code> #include <iostream> #include <assert.h> template<class TReturn, class TInstance, class TMethod> struct Functor { Functor(TInstance instance, TMethod method) : _instance(instance), _method(method) {} TReturn execute() const { return (_instance->*_method)(); } TInstance _instance; TMethod _method; }; template<class TInstance, class TMethod> struct Functor<void, TInstance, TMethod> { Functor(TInstance instance, TMethod method) : _instance(instance), _method(method) {} void execute() const { (_instance->*_method)(); } TInstance _instance; TMethod _method; }; template <class TInstance> Functor<void, TInstance*, void (TInstance::*)()> makeFunctor(TInstance* instance, void (TInstance::*method)()) { return Functor<void, TInstance*, void (TInstance::*) ()>(instance, method); } template <class TInstance> Functor<void, const TInstance*, void (TInstance::*)() const> makeFunctor(const TInstance* instance, void (TInstance::*method)() const) { return Functor<void, const TInstance*, void (TInstance::*)() const>(instance, method); } template <class TReturn, class TInstance> Functor<TReturn, TInstance*, TReturn (TInstance::*)()> makeFunctor(TInstance* instance, TReturn (TInstance::*method)()) { return Functor<TReturn, TInstance*, TReturn (TInstance::*) ()>(instance, method); } template <class TReturn, class TInstance> Functor<TReturn, const TInstance*, TReturn (TInstance::*)() const> makeFunctor(const TInstance* instance, TReturn (TInstance::*method)() const) { return Functor<TReturn, const TInstance*, TReturn (TInstance::*) () const>(instance, method); } class X { public: void test1() { std::cout << "X::test1()" << std::endl; } void test1const() const { std::cout << "X::test1const()" << std::endl; } int test2() { return 1024; } int test2() const { return 2048; } int test3() const { return 4096; } }; /// none const tests: void functor_test1(X& myx) { makeFunctor(&myx, &X::test1).execute(); assert(makeFunctor<int>(&myx, &X::test2).execute() == 1024); } /// const tests: void functor_test2(const X& myx) { makeFunctor(&myx, &X::test1const).execute(); assert(makeFunctor<int>(&myx, &X::test2).execute() == 2048); assert(makeFunctor<int>(&myx, &X::test3).execute() == 4096); } int main() { X myx; functor_test1(myx); functor_test2(myx); return 0; }//main </code> -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|