aquavitae
August 17th, 2007, 12:04 PM
Hi all
I'm attempting to write a class that basically does the following:
template<typename T>
class my_class {
T m_val;
public:
void set(T value) {
m_val = val;
}
};
There's more to it, but thats the part I'm stuck on. What I want to do is to allow the class that owns an instance of my_class to intervene in the set() function and permit the change. For example, only allow the value to be set if a certain external condition is met. Basically, I want to do this:
void my_class::set(T value) {
m_value = request_change(value, m_value);
}
class owner_class {
my_class<int> a;
my_class<char> b;
int request_change_a(int, int) {
...
}
};
Obviously the code I wrote won't work as I want because request_change_a cannot be seen from within my_class, and even if it could it would't know which instance to work with.
Two options I thought of are to either use boost::signals or to put a pointer to request_change as a template argument of my_class.
The problem with boost::signals is that it will allow any number of request_change functions to be used on one instance of my_class (I only want one), and it will also allow any function that can see the instance of my_class access to it (I want it to be accessible only from owner_class).
The problem with a template argument is that I don't think its syntactically possible to use a function pointer as a template argument.
Does anyone have any other ideas, or know how to use a function pointer as a template argument?
I'm attempting to write a class that basically does the following:
template<typename T>
class my_class {
T m_val;
public:
void set(T value) {
m_val = val;
}
};
There's more to it, but thats the part I'm stuck on. What I want to do is to allow the class that owns an instance of my_class to intervene in the set() function and permit the change. For example, only allow the value to be set if a certain external condition is met. Basically, I want to do this:
void my_class::set(T value) {
m_value = request_change(value, m_value);
}
class owner_class {
my_class<int> a;
my_class<char> b;
int request_change_a(int, int) {
...
}
};
Obviously the code I wrote won't work as I want because request_change_a cannot be seen from within my_class, and even if it could it would't know which instance to work with.
Two options I thought of are to either use boost::signals or to put a pointer to request_change as a template argument of my_class.
The problem with boost::signals is that it will allow any number of request_change functions to be used on one instance of my_class (I only want one), and it will also allow any function that can see the instance of my_class access to it (I want it to be accessible only from owner_class).
The problem with a template argument is that I don't think its syntactically possible to use a function pointer as a template argument.
Does anyone have any other ideas, or know how to use a function pointer as a template argument?