
please wait
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_type
of ItemData
would be an enum.
|
|
const ItemData ItemDataArray[] = { Apple, Steak, ...};
is there any clever way I can set up my code so that I both define the initial enum value (i.e. starting at 0), AND the matching parameters for that item, in one const array? |
|
|
… confusing that wood and apple are stored as ItemData. How are they related? |
bool _food;
… it seems to be classifying food and non-food items but OP himself/herself will be able to tell us more … kind of a miss that the language has 5 different arrays but still does not have an enum that ties the strings and the values together … |
Why are C++ constructors so rigid? Why don't we have flexible means to create objects in the language itself? Interestingly, seeking an answer to this question takes us directly to fundamental decisions about C++'s type system. To find out why a statement such as Base* pB = new Derived; is so rigid, we must answer two related questions: What is a class, and what is an object? This is because the culprit in the given statement is Derived, which is a class name, and we'd like it to be a value, that is, an object. In C++, classes and objects are different beasts. Classes are what the programmer creates, and objects are what the program creates. You cannot create a new class at runtime, and you cannot create an object at compile time. Classes don't have first-class status: You cannot copy a class, store it in a variable, or return it from a function. In contrast, there are languages in which classes are objects. In those languages, some objects with certain properties are simply considered classes by convention. Consequently, in those languages, you can create new classes at runtime, copy a class, store it in a variable, and so on. If C++ were such a language, you could have written code like the following:
That is, we could pass a variable of type Class to the new operator. In such a paradigm, passing a known class name to new is the same as using a hardcoded constant. Such dynamic languages trade off some type safety and performance for the sake of flexibility, because static typing is an important source of optimization. C++ took the opposite approach, sticking to a static type system, yet trying to provide as much flexibility as possible in this framework. |
|
|
|
|
|
|
|
|
|
|
|
|
Using the tortuous object factory that was suggested as a solution for this would be utterly asinine. |