This question already has answers here:
Dynamic Variable Naming and Reference (ColdFusion)
(2 answers)
Closed 7 years ago.
I have a struct called params inside are keys ch_1, ch_2, ch_3, etc
I am inside a loop that loops over query and use the id column to access the ch_(key) value
for(ck in checklist)
{
n_checklist.status = params.ch_(ck.key_value);
}
How would I write this?
I was thinking params.ch_#ck.key# would work but it doesn't. I want to create a property name using another variable value.
You can use associative array notation to accomplish this.
for (ck in checklist) {
n_checklist.status = params['ch_' & ck.key_value];
}
Related
This question already has answers here:
Why do Python classes inherit object?
(6 answers)
Closed 6 years ago.
I am learning to make classes using template codes and one of them is coded with what looks like inheritance syntax:
class KNNLearner(object):
What is the purpose of (object) here?
In python everything is an object, an object is an object. You can also create Meta-classes to change some behavior of your objects/object to implement something.
In you case KNNLearner(object) the (object) permits you to pass the class that you want to KNNLearner to inherit.
This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
So I have a pointer SparseMatrix *mat which holds values of the sparsematrix in a triplet format from a file. After I get the values, I would like to be able to just print it out using a print function I created in the SparseMatrix() class. However, I realize that this works if it wasn't a pointer and just 'mat'. I feel like this is something simple but I just cannot wrap my head around it. Any suggestions? Forgive me if this is an extremely noob question. Also, the reason I'm even using a pointer in the first place is because I am trying to visualize a sparsematrix using QT and my professor had given the basic backbone of the code. In that code, a private variable, SparseMatrix *mat was given.
Via pointers you can access your functions using ->.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to instantiate objects from a string holding their class name?
I've written a Vbo template class to work with vertex buffer objects in opengl. I'm writing for multiple platforms in c++.
I'd like to set the type from a config file at runtime.
e.g.
<vbo type="bump_vt" ... />
Vbo* pVbo = new Vbo<bump_vt>(...);
Is there some way I can do this without a large if else block e.g.
Vbo* pVbo;
if( sType.compareTo("bump_vt") == 0 )
pVbo = new Vbo<bump_vt>(...);
else if
...
thanks
C++ doesn't allow that, because types are resolved during compilation.
But you may use std::map to simplify the process.
This question already has answers here:
Get attribute by name
(5 answers)
Closed 4 years ago.
In the vein of more impossible-but-is-it-really questions:
Is it possible to access the member variable of a class, where the variable's name is stored in a string?
class Test
{
public:
int test = 0;
}
string name = "test"; // let's assume we know test is an int.
Any chance of getting the value of test, using the string?
One bit of cheating not allowed:
enum vartype {
INT,
..
}
No forcing the class to register all its variables in a std::map<string, std::pair<vartype, void*> >.
All other tricks welcome.
Thanks!
No.
To do this, you need to provide some mapping between member variables and the string names by which you intend to access them.
In the realm of really ugly kluges, you could build the program with debug information and have it use that to find the location of the variable in the same way a debugger would. But other than that, you're out of luck. C++ doesn't do reflection.
About why it's not available in C++ and an alternative: http://en.allexperts.com/q/C-1040/eval-function-javascript-C.htm
It's possible in MATLAB though...
As a very simple example, if you have a matrix updation to do, which goes like:
M1=1;
M2=2;
M3=3;
And you would prefer that the variable names could be altered so that you could use a for loop, then it can also be done this way:
for i=1:3
eval(['M' num2str(i) '=' num2str(i)]);
end
I used to do this in Actionscript. Was really glad to find that it's available in Matlab too
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Iterate through struct variables.
So I have a header file and a code file. The class is representation of a View that will be queried from stored proc. For each col. in view there is one data member in class.
Currently in code we have something like this:
Load( Reader reader)
{
m_col1 = reader("m_col1");
m_col2 = reader("m_col2");
..
}
How can I write a code that will iterate through member variables and give me code like:
Load( Reader reader)
{
For (each str in ArrayOfMemberVariables)
variableLValue(str) = reader(str); // str = m_col1, m_col2 ...
}
The C++ reflection question has been brought up several times. Unfortunately, it's not possible unless you manage the metadata yourself. See this question for more details.
If you mean declaring variables names dynamicly like in PHP for example (using other variable names), you can't do that in C++.
In C++ you don't have the notion of reflection like in Java where you can introspect the variables of your class and code around that to do things like serialization with knowing in advance the class members.