473,505 Members | 13,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template fns in non-template class

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
2 1214
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4398
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
3727
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
12189
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
1
6885
by: Markus Ernst | last post by:
Hi I wrote a function that "normalizes" strings for use in URLs in a UTF-8 encoded content administration application. After having removed the accents from latin characters I try to remove all...
4
5273
by: bwmiller16 | last post by:
Guys - I'm doing a database consistency check for a client and I find that they're building unique indexes for performance/query reasons where they could be using non-unique indexes. Note...
2
6089
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
2321
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
12635
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
3789
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
29791
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.