Syntax for template for member function of multiple classes?

Discussion in 'C++' started by Howard, Jan 5, 2007.

  1. Howard

    Howard Guest

    Hi,

    I have a function in three unrelated but similar classes. The code in the
    member functions is identical for all three classes. What I want is to make
    a template which defines the function, implemented as a non-static member of
    each class. Aside from this function, there is no reason the three classes
    should inherit from a common base class, which is why I thought a template
    would be good. But I can't figure out the syntax for a template which
    specifies itself as a member function of the specified class. Also, how
    would each class declare the member function?

    I tried variations of this, but no luck:

    template <class T>
    void <T>::foo() // syntax errors here
    {
    // ...do stuff here...
    }

    class A
    {
    void foo<A>();
    };

    class B
    {
    void foo<B>();
    };

    class C
    {
    void foo<C>();
    };

    Thanks,
    -H
     
    Howard, Jan 5, 2007
    #1
    1. Advertising

  2. Howard

    red floyd Guest

    Howard wrote:
    > Hi,
    >
    > I have a function in three unrelated but similar classes. The code in the
    > member functions is identical for all three classes. What I want is to make
    > a template which defines the function, implemented as a non-static member of
    > each class. Aside from this function, there is no reason the three classes
    > should inherit from a common base class, which is why I thought a template
    > would be good. But I can't figure out the syntax for a template which
    > specifies itself as a member function of the specified class. Also, how
    > would each class declare the member function?
    >
    > I tried variations of this, but no luck:
    >
    > template <class T>
    > void <T>::foo() // syntax errors here
    > {
    > // ...do stuff here...
    > }
    >
    > class A
    > {
    > void foo<A>();
    > };
    >
    > class B
    > {
    > void foo<B>();
    > };
    >
    > class C
    > {
    > void foo<C>();
    > };
    >
    >


    I think your best bet is something like this:

    template< class T >
    class FooClass : public T
    {
    public: // I asume you wanted this
    void foo();
    };

    Otherwise, I think you are pretty much oiut of luck
     
    red floyd, Jan 5, 2007
    #2
    1. Advertising

  3. Howard

    psp Guest

    Howard wrote:
    > Hi,
    >
    > I have a function in three unrelated but similar classes. The code in the
    > member functions is identical for all three classes. What I want is to make
    > a template which defines the function, implemented as a non-static member of
    > each class. Aside from this function, there is no reason the three classes
    > should inherit from a common base class, which is why I thought a template
    > would be good. But I can't figure out the syntax for a template which
    > specifies itself as a member function of the specified class. Also, how
    > would each class declare the member function?
    >
    > I tried variations of this, but no luck:
    >
    > template <class T>
    > void <T>::foo() // syntax errors here
    > {
    > // ...do stuff here...
    > }
    >
    > class A
    > {
    > void foo<A>();
    > };
    >
    > class B
    > {
    > void foo<B>();
    > };
    >
    > class C
    > {
    > void foo<C>();
    > };
    >
    > Thanks,
    > -H

    I think declaring this function as a friend of all 3 classes is
    appropriate. Then you can define this function template wherever you
    want.
     
    psp, Jan 5, 2007
    #3
  4. Howard

    psp Guest

    Howard wrote:
    > Hi,
    >
    > I have a function in three unrelated but similar classes. The code in the
    > member functions is identical for all three classes. What I want is to make
    > a template which defines the function, implemented as a non-static member of
    > each class. Aside from this function, there is no reason the three classes
    > should inherit from a common base class, which is why I thought a template
    > would be good. But I can't figure out the syntax for a template which
    > specifies itself as a member function of the specified class. Also, how
    > would each class declare the member function?
    >
    > I tried variations of this, but no luck:
    >
    > template <class T>
    > void <T>::foo() // syntax errors here
    > {
    > // ...do stuff here...
    > }
    >
    > class A
    > {
    > void foo<A>();
    > };
    >
    > class B
    > {
    > void foo<B>();
    > };
    >
    > class C
    > {
    > void foo<C>();
    > };
    >
    > Thanks,
    > -H

    I think declaring this function as a friend of all 3 classes is
    appropriate. Then you can define this function template wherever you
    want.
     
    psp, Jan 5, 2007
    #4
  5. Howard

    bjeremy Guest

    Howard wrote:
    > Hi,
    >
    > I have a function in three unrelated but similar classes. The code in the
    > member functions is identical for all three classes. What I want is to make
    > a template which defines the function, implemented as a non-static member of
    > each class. Aside from this function, there is no reason the three classes
    > should inherit from a common base class, which is why I thought a template
    > would be good. But I can't figure out the syntax for a template which
    > specifies itself as a member function of the specified class. Also, how
    > would each class declare the member function?
    >



    You can use forwarding to do this:

    template <class T>
    void foo()
    {
    T *t = new T;
    cout << "I am a: " << typeid(t).name() << endl;

    delete t;
    }

    class A
    {
    public:
    void doTheFoo() {foo<A>();}
    };

    class B
    {
    public:
    void doTheFoo() {foo<B>();}
    };

    class C
    {
    public:
    void doTheFoo(){foo<C>();}
    };

    int main(int argc,char **argv)
    {

    A a;
    a.doTheFoo();

    B b;
    b.doTheFoo();

    C c;
    c.doTheFoo();

    return 0;
    }
     
    bjeremy, Jan 5, 2007
    #5
  6. Howard

    Grizlyk Guest

    Howard wrote:

    > template <class T>
    > void <T>::foo() // syntax errors here
    > {
    > // ...do stuff here...
    > }


    You are trying to declare a function foo() of class T outside of class
    T declaration (T{};). This is wrong.

    > I have a function in three unrelated but similar classes. The code in the
    > member functions is identical for all three classes. What I want is to make
    > a template which defines the function, implemented as a non-static member of
    > each class.

    In most cases, your desire is not good, because you are not using any
    desing questions. Your conclusion "code in the member functions is
    identical" is not design question, you must not have a look to the
    classes in the manner while creating them.

    If design does not matter to you here, just make the function outside
    of classes, in any namespace.
     
    Grizlyk, Jan 12, 2007
    #6
  7. Howard

    Howard Guest

    "red floyd" <> wrote in message
    news:Y4ynh.14047$...
    > Howard wrote:
    >> Hi,
    >>
    >> I have a function in three unrelated but similar classes. The code in
    >> the member functions is identical for all three classes. What I want is
    >> to make a template which defines the function, implemented as a
    >> non-static member of each class. Aside from this function, there is no
    >> reason the three classes should inherit from a common base class, which
    >> is why I thought a template would be good. But I can't figure out the
    >> syntax for a template which specifies itself as a member function of the
    >> specified class. Also, how would each class declare the member function?
    >>
    >> I tried variations of this, but no luck:
    >>
    >> template <class T>
    >> void <T>::foo() // syntax errors here
    >> {
    >> // ...do stuff here...
    >> }
    >>
    >> class A
    >> {
    >> void foo<A>();
    >> };
    >>
    >> class B
    >> {
    >> void foo<B>();
    >> };
    >>
    >> class C
    >> {
    >> void foo<C>();
    >> };
    >>

    >


    I found a solution: I made a small class which holds just the data which is
    operated on by those three functions, and made a single member function of
    that new class to replace the three functions. Then I replaced that common
    data with an instance of the new class (inside each of my three other
    classes), and replaced calls to the old member functions with calls into
    that new class's member function.

    It occurs to me that what I was trying to do was more like a macro than a
    template, and probably not the best design. It's working fine now.

    Thanks all,
    -H
     
    Howard, Jan 12, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BigMan
    Replies:
    1
    Views:
    535
  2. Replies:
    1
    Views:
    481
    Victor Bazarov
    Jul 20, 2005
  3. Peng Yu
    Replies:
    3
    Views:
    801
    Thomas J. Gritzan
    Oct 26, 2008
  4. Hicham Mouline
    Replies:
    0
    Views:
    450
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    436
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page