This question is a continuation of an earlier one found at: How to template'ize variable NAMES, not types?
Let's say one has the following code:
struct VAR_TYPE{
public:
bool is_fixed;
double value; // Numerical value
std::string name; // Description of variable (to identify it by name)
int reference_counter;
/* ect. */
};
struct NODE{
public:
VAR_TYPE X, Y, Z;
/* ect. */
};
class MyClass{
public:
std::vector <NODE_ptr> node; // shared ptr, using boost::shared_ptr
// what I have now
void set_variable_X(int &i, double &P) { node[i]->X.value = P; }
void set_variable_Y(int &i, double &P) { node[i]->Y.value = P; }
void set_variable_Z(int &i, double &P) { node[i]->Z.value = P; }
// What I want to replace all 3 members with:
<class T, class N>
void set_variable( int &i, double &P, /* ??? */ ) { /* ??? */ }
/* ect. */
};
I'm not sure what would go in areas where '???' is written. Borrowing pseudocode used in the aforementioned link, I would like to something to the affect of
main(){
MyClass S;
double x1, y1, z1;
int index;
// set x1, y1, z1, index
S.set_variable( index, x1, &MyClass::node::X ); // in essence, what I want
S.set_variable( index, y1, &MyClass::node::Y );
S.set_variable( index, z1, &MyClass::node::Z );
};
I've tried a few ideas, but errors run a muck. I think the issues lies with the fact I am using a boost shared pointer and/or std::vector. Anyone have an idea what the problem, and a suitable solution, are? One options I have been working with is (but it does not use the calling convention I identified in the int main() above):
template < class T, class N >
void MyClass::set_reference( int &i, double &P,
T NODE::* MemPtr,
N VAR_TYPE::* ValPtr)
{
*MemPtr[i].*ValPtr.value = P; // doesn't work work
};