By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,141 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,141 IT Pros & Developers. It's quick & easy.

Template fns in non-template class

P: n/a
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 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Feb 1, 1:38 pm, garfunkelor...@googlemail.com 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

Feb 1 '07 #2

P: n/a
On 1 Feb, 13:01, "Erik Wikström" <eri...@student.chalmers.sewrote:
On Feb 1, 1:38 pm, garfunkelor...@googlemail.com 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 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.