Template fns in non-template class

Discussion in 'C++' started by garfunkelorama@googlemail.com, Feb 1, 2007.

  1. Guest

    Hi all,

    I'm sure there is a simple solution to this but it's beyond me. Please
    can someone help?
    You have a class like this:

    class A
    {
    public:
    template<typename T>
    void f(key &k, T& t)
    {
    // need to store the address of t
    }

    void g(key &k)
    {
    // somehow get the address of the t variable above from the
    supplied key
    }
    };

    and then usage could be:

    A a;
    string str = "hello";
    double d = 3.2;
    int i = 2;

    a.f(1, str);
    a.f (2, d);
    a.f(3, i);
    //....

    a.g(); //do something to alter values of str, d, i

    How can you store the variable t but make it accessible to the rest of
    the class ? You cannot have any variable in the class mention T as
    it's not a template class. No void ptr if possible as that has its
    associated headaches.
    The boost "variant" library looks helpful but still i can't see how.
    Other than resort to scrapping the template and just have regular int,
    double and string pointers within A.

    Thank you.
    G.
     
    , Feb 1, 2007
    #1
    1. Advertising

  2. On Feb 1, 1:38 pm, wrote:
    > Hi all,
    >
    > I'm sure there is a simple solution to this but it's beyond me. Please
    > can someone help?
    > You have a class like this:
    >
    > class A
    > {
    > public:
    > template<typename T>
    > void f(key &k, T& t)
    > {
    > // need to store the address of t
    > }
    >
    > void g(key &k)
    > {
    > // somehow get the address of the t variable above from the
    > supplied key
    > }
    >
    > };
    >
    > and then usage could be:
    >
    > A a;
    > string str = "hello";
    > double d = 3.2;
    > int i = 2;
    >
    > a.f(1, str);
    > a.f (2, d);
    > a.f(3, i);
    > //....
    >
    > a.g(); //do something to alter values of str, d, i
    >
    > How can you store the variable t but make it accessible to the rest of
    > the class ? You cannot have any variable in the class mention T as
    > it's not a template class. No void ptr if possible as that has its
    > associated headaches.
    > The boost "variant" library looks helpful but still i can't see how.
    > Other than resort to scrapping the template and just have regular int,
    > double and string pointers within A.


    You have to cast it to void*, something like this ought to work:

    class A {
    std::map<key, void*> map_;
    public:
    template<class T>
    void f(key k, T& t) {
    map_[k] = static_cast<void*>(&t);
    }
    void* g(key k) {
    return map_[k];
    }
    };

    The problem with this is that the user calling g() have to know the
    type of the object pointed to, but if it's known then you can cast it
    back to a pointer of string, int, or whatever.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 1, 2007
    #2
    1. Advertising

  3. Guest

    On 1 Feb, 13:01, "Erik Wikström" <> wrote:
    > On Feb 1, 1:38 pm, wrote:
    >
    >
    >
    >
    >
    > > Hi all,

    >
    > > I'm sure there is a simple solution to this but it's beyond me. Please
    > > can someone help?
    > > You have a class like this:

    >
    > > class A
    > > {
    > > public:
    > > template<typename T>
    > > void f(key &k, T& t)
    > > {
    > > // need to store the address of t
    > > }

    >
    > > void g(key &k)
    > > {
    > > // somehow get the address of the t variable above from the
    > > supplied key
    > > }

    >
    > > };

    >
    > > and then usage could be:

    >
    > > A a;
    > > string str = "hello";
    > > double d = 3.2;
    > > int i = 2;

    >
    > > a.f(1, str);
    > > a.f (2, d);
    > > a.f(3, i);
    > > //....

    >
    > > a.g(); //do something to alter values of str, d, i

    >
    > > How can you store the variable t but make it accessible to the rest of
    > > the class ? You cannot have any variable in the class mention T as
    > > it's not a template class. No void ptr if possible as that has its
    > > associated headaches.
    > > The boost "variant" library looks helpful but still i can't see how.
    > > Other than resort to scrapping the template and just have regular int,
    > > double and string pointers within A.

    >
    > You have to cast it to void*, something like this ought to work:
    >
    > class A {
    > std::map<key, void*> map_;
    > public:
    > template<class T>
    > void f(key k, T& t) {
    > map_[k] = static_cast<void*>(&t);
    > }
    > void* g(key k) {
    > return map_[k];
    > }
    >
    > };
    >
    > The problem with this is that the user calling g() have to know the
    > type of the object pointed to, but if it's known then you can cast it
    > back to a pointer of string, int, or whatever.
    >
    > --
    > Erik Wikström- Hide quoted text -
    >
    > - Show quoted text -


    Thanks, I think that is the only way too.
     
    , Feb 1, 2007
    #3
    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. kitcha
    Replies:
    3
    Views:
    2,986
    Charles Bailey
    Nov 24, 2005
  2. Matt Graham
    Replies:
    0
    Views:
    573
    Matt Graham
    Jul 21, 2003
  3. christopher diggins
    Replies:
    16
    Views:
    773
    Pete Becker
    May 4, 2005
  4. Steve Canfield

    member fns w/o args?

    Steve Canfield, Jul 9, 2004, in forum: Python
    Replies:
    2
    Views:
    281
    Christopher T King
    Jul 9, 2004
  5. nguillot
    Replies:
    5
    Views:
    548
Loading...

Share This Page