I'm having some trouble converting the following code from c++ to c# because of pointers.
Basically I have a
STATE** State;
States = new STATE* [max_states];
for (int i=0; i < max_states; i++) {
States[i] = new STATE(max_symbols);
}
If this was some double array I would say
STATE[][] States;
States = new STATE[max_states][];
for (int i = 0; i '<' max_states; i++) {
States[i] = new STATE[max_symbols];
}
But the problem is the c++ code is not working "as" I expected it to.
States[i] = new STATE(max_symbols);
Has some strange behavior that for example allows
States[cur_state]->set_开发者_如何转开发recur_out(k);
what exactly am I not seeing? This might be a beginner c++ question. Sorry if I don't make any sense at all =)
it is not a 2d-array, but a 1d-array containing pointers to single elements...
new STATE(max_symbols)
constructs a single STATE object, calling the constructor which takes a single argument (in this case max_symbols).
i dont have that much clue of C#, but the following should be the correct representation of the C++ code in C#:
STATE[] States;
States = new STATE[max_states];
for (int i = 0; i '<' max_states; i++) {
States[i] = new STATE(max_symbols);
}
This is simply an array of pointers. In C# that would be a one-dimensional array:
STATE[] States = new STATE[max_states];
for (int i = 0; i < max_states; i++) {
States[i] = new STATE(max_symbols);
}
The “strange behavior” that you are seeing in the C++ is simply the C++ way of accessing a member of a pointer to a type:
Type* x;
x->y();
// is the same as:
(*x).y();
// and corresponds to:
Type z;
z.y();
精彩评论