1

Let's assume I have a class Object with one method which simply reports the ID number of the instance. Normally, I would hardcode the instantiation of the Object class like "Object obj_1" and the calling of the method like "obj_1.report"

My question is how do I instantiate objects procedurally, for example I want to create n number of Objects, going obj_1, obj_2 and so on till obj_n. Natural I am not asking about the actual loop but about how to instantiate the class using a variable, but taking the value of the variable instead of its name and adding it to the obj_ prefix. Perhaps with casting? Also how do I procedurally call the methods of specific instances by specifying only the ID. I think both the instantiation and the method calling will work in the same way, however as a newbie I have a hard time figuring how exactly to do it on the go instead of being hardcoded.

Thanks in advance!

EDIT: I am interested in c++ syntax

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
dtech
  • 47,916
  • 17
  • 112
  • 190
  • Have you heard about arrays? Or `std::vector`? – Joachim Sauer Oct 12 '11 at 13:55
  • Yes, I am familiar with the basics of arrays, but my problem is related to using the value of a variable as the name of the instance. How to replace "Object obj_1" with "Object obj_(VAR)" where VAR is 1. – dtech Oct 12 '11 at 14:10
  • and how would that be different from using an array `obj` where `VAR` is the index? – Joachim Sauer Oct 12 '11 at 14:11
  • I still don't get your point. I could use an array to store all the instances, but what I need is the actual syntax to instantiate a class object using the value of a variable or the index of an array as a name, plus the prefix. – dtech Oct 12 '11 at 14:35
  • 1
    **why**? What advantage does that have? The **names** don't mean anything at runtime and all you get from this is a complicated way to emulate what an array does. Can you describe the *actual requirement* that requires you to "programatically" create such variables? Hint: when I ask "How would that be different?" it's **not** a rhetorical question. I *actually* hoped you could tell me the difference. – Joachim Sauer Oct 12 '11 at 14:37
  • I know I can simply instantiate an array of Objects and that it will be less to no different form what I am asking for. But for example, if I create a 1000 objects in one array and later decide I need to remove 300 of them, I wouldn't be able to do that, since arrays are fixed length after initialization. I could use std::vector to get a resizable container for my objects. But what if I want to create 1000 A Objects and 2000 B Objects - I will have to use two vectors, but then I'd have different IDs of A and B objects instead of total of 3000 IDs for all Objects. – dtech Oct 13 '11 at 07:38
  • And it gets more complex if I want to create more C and D Objects, I do realize I can add the lengths of all vectors to get an ID for all A B C and D Objects, but I feel like it gets too complex. I need to be able to either call an Object method either by is ID or its name, for example obj_B_1311 whose ID is 2311. Get my idea? I do realize this might not be the best way to do things but I am fairly noob. Last but not least, I still need to know the syntax how can I use values of variables to cast as text plus adding prefixes and suffixes in order to use as variable names or values. Thanks :) – dtech Oct 13 '11 at 07:45
  • Trust me: you're going at this wrong. Having thousands of variables is **not** easier or less complex than having a few arrays. And regarding your arguments: How would you "remove 300 elements" if those elements were variables? – Joachim Sauer Oct 13 '11 at 07:59
  • I don't know, and why would I remove variables, since they exist only in the scope and only for as long as they are needed, unless they are global, which if so would be for a good reason, but I know how to remove 300 instances of a class. I do realize using vectors or arrays is more economical since it uses only one pointer for as many objects as I may want to, but is it that bad of an idea to have dedicated pointers for every instance instead? – dtech Oct 13 '11 at 08:06
  • **Yes**, it *is* a bad idea, since you *rarely* treat all of those values different in some way. And if you all treat them in a similar way, then you should put them in a common data structure so that you can act on them as a set. – Joachim Sauer Oct 13 '11 at 08:08
  • But why is it a bad idea? I am not nitpicking, just want to know why. After all a pointer is the size of an int, it is not that much of a memory overhead considering Objects also have string names and potentially other data as well. I am not against efficiency but sometimes it is a good idea to sacrifice some efficiency in the name of versatility, after all we are long past the "640kb outta be enough for everyone" era :) – dtech Oct 13 '11 at 08:22
  • how do objects have "string names"? They don't. An object doesn't have a name. I just don't see a single advantage in what you're trying to do. It's a common train of thought by beginners, but the sooner you learn that it's leading you nowhere (except maybe to an alternative definition of an array), the better. – Joachim Sauer Oct 13 '11 at 08:28
  • Well it may be bad lingo, I am not a native English speaker, and am new to programing terminology, but a class object can have a string member containing its name. I do realize there is a difference between creating one object and moving it through data, but aren't there scenarios of usage, where it is better to have multiple objects with their own dedicated data. For example it would be more efficient to have struct that contains ID and name data and move a single object along it to manipulate it, but having every entry as separate instance could have benefits, or can't it? – dtech Oct 13 '11 at 08:39
  • An example - a simple address book, the smart way to do it would be to have a data structure for all the entries and one class object to manipulate that data structure. I am aware it makes no sense to create every entry as a separate class object instead of using one data structure for all entries, because that would use more memory and an address book simply has nothing to benefit from doing so, but I have those blurry concepts in my mind where having individual instances holding on their personal data may have advantages in terms of functionality and versatility. Does this make any sense? – dtech Oct 13 '11 at 08:44
  • Lets assume I have thousands of objects which not only hold data but interact with each other. I could program those interactions in an external function that reads entry data from a monolithic data structure, performs the interactions and injects the results back into the data structure, but this is old procedural C programing, the object oriented way would be to have the objects with the interaction functionality encapsulated inside them. – dtech Oct 13 '11 at 08:50
  • I'm sorry, but I no longer have any idea what you're talking about and how it relates to your original question. Also: this format is **definitely** not suited for extended discussions such as this one. Just this much: I think you are mistaken if you think that an array of objects is somehow fundamentally different from many distinct instances of that object: it's the same thing. Instances in an array are no less "real" than other instances. – Joachim Sauer Oct 13 '11 at 08:52
  • I am sorry too, it is probably me, explaining it wrong. So what you are saying is there is absolutely no benefit from having a direct pointer to every class instance, and it is better to store those in arrays or vectors? Won't it be faster to access only the pointer instead of the pointer and then the actual index of that object, which is an offset from the pointer? Anyway, Anthony has actually answered my question but in Java syntax, I was hoping for c++ version, but your questions on arrays and on why I'd want to do this have hurled my question into this inappropriate form of discussion. – dtech Oct 13 '11 at 10:38

1 Answers1

0

The best solution I can think of is to store your objects in a Map, with your IDs ("obj_1", "obj_2", etc) being the keys that refer to those instances.

Java (since you didn't specify a language) sample code would look something like this:

Map<String, Object> objMap = new HashMap<String, Object>();
for(int i = 1; i <= 10; i++) {
    objMap.put("obj_" + 1, new Object());
}

for(int i = 1; i <= 10; i++) {
    if(objMap.containsKey("obj_" + i))
        objMap.get("obj_" + i).report();
}
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
  • It is my fault for not mentioning I am learning C++, in fact I have added c++ as a tag, but the site said 5 tags maximum so I deleted it. Anyway, I am specifically asking for C++ syntax, still thanks for your rapid reply :) – dtech Oct 12 '11 at 13:36
  • @user991484: you should have removed one of the other tags. [tag:text] for example doesn't really add much useful information, so I took the liberty to replace it with [tag:c++] for you. – Joachim Sauer Oct 12 '11 at 14:11