Unique class names (C++)
I want to create an instance of a class with name "X", where "X" is defined by the user.
So that, if they want to input information for a new division of a company, they cold input "Marketing" and the program would initialize an instance of the Division class with the name "Marketing". Possible? |
Nope, not possible. That is dynamic run-type type-name binding. C++ is a low-level language and every thing is statically configured at runtime, for efficientcy reasons.
To answer this question you really need to think about what you mean when you say that a class has a name. Do you mean that you want to instaniate an object of the Division class which contains an identifying string "Marketing" enbedded in it? |
Not only not possible, not desirable (that I can see). If you are relying on the user to name the class you are creating, then you probably have not spent enough time analyzing your project from an OO perspective.
Also keep in mind that class names are for programmers; the classes should be invisible to the user. It really seems as though bm17's suggestion is more likely, that you have a Division that will be labeled as "Marketing" via user input when the object is instantiated. Is that right? Or why, exactly, would you want a user to name your class? |
Sorry, I take that back upon rereading your post. Clearly you want an instance of Division instantiated with the name "Marketing", for example. That can easily be done with a constructor that takes the division name as one of its arguments. Piece of cake.
|
OK, I have taken a class in C++ and it was believe or not the first one offered at HCC. With that, we did as a class project a POS/INventory software for an imaginary bookstore. Each book was an object of the Book class. Now, being beginers our project had us initialize an array of twenty of these Books, and that was all inventory could do. What I am trying to do is something similar...
Imagine a watch store, with a similar inventory controling/monitoring software system. Each watch is stored in a class object. When I want to add a new watch, I want to create a new object of that class. So if I added Seiko to my inventory, I could create an object of the Watch class named Seiko, with all the member functions of the watch class. So I could use like.... seiko.getRetail(); Is THIS possible is what I was asking. -TR PS ; Like this.... Code:
class Watch |
No, it can not be done. Variable names, in the sense that you are refering to, exist only for the compiler to refer to while producing code. They do not exist outside of that. At best you can produce a global symbol at compile time. You can not produce new symbols at runtime.
|
I really think you need to ask yourself why you want to do this. If a watch needs to be identified by brand name, then that should be a member variable within the class. For example:
Code:
class Watch |
All times are GMT -5. The time now is 07:08 PM. |