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.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-2008, 07:52 PM
Iris-und-Thomas-Lehmann@t-online.de
 
Posts: n/a
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! ]

Reply With Quote
  #2 (permalink)  
Old 08-19-2008, 10:25 PM
Thomas Lehmann
 
Posts: n/a
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! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:20 AM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Online Advertising | Buy Anything On eBay | Accounting Warsaw | Remortgages | File Sharing & Mirroring



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114