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. 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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>
|
by: puzzlecracker |
last post by:
is it even possible or/and there is a better alternative to accept
input in a nonblocking manner?
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |